1. Create model, migrate, controller (you can use --api to remove create and edit because it's API after all), route and fill the details in a regular json response way. Add auth as well to login later.
2. If you don't have, you'll need passport. You'll need to generate key, so:
php artisan passport:keys
php artisan passport:keys
Keep in mind that I'm building it to use with Axios at Nuxt later on. So, Axios will be a must as well.
3. Once everything is created, you can attempt a login by checking routes:
php artisan route:list or php artisan r:l
php artisan route:list or php artisan r:l
4. Now we look for /oauth/token route. Once we've got it, we can open our Postman and use generated.
Inside the Postman we have to Create a new connection, if you don't know how to do it, google it ;) / youtube it. I'm using POST method and here are details:

So, as you see in my POST method I'm adding a URL with /oauth/token at the very end.
In the Body I have to put 5 details grant_type is just word password, client_id and client_secret are from your passport, username and password are your login credentials. If you've done everything right, you should see result something like that:

You'll need access_token to login and use CRUD functionality later on. So, to use login you have to copy the content of that token from inside the brackets switch to tab Authorization and from Type choose Bearer Token. Next on the right in the field Token you have to past in content of your access_token.
Now, if you know your route for let say products, put it in the URL field and change the method to get. If you've done everything right, you should get your results. In my case I've got my test products:
As you can see id 4 and 5 are null. This is my fault, I should add in my post method in Headers key and value which I didn't straight away like this:

If you won't add it like I've shown when you POST to save items, your database will be receiving empty fields.
Post method to save will require either form-data or raw. My row here was as follow:
Post method to save will require either form-data or raw. My row here was as follow:
So, how you see how to get out token to login each time and request data and how to post. You should be able to create your front side client now.
No comments:
Post a Comment