Laravel 8 Tutorial - CRUD API Using Resources

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome back to the level 8 tutorial in this video we are going to learn about crud api using resources so let's see how can we create cred api using resources in laravel 8 so first of all let's create a new library project so for that switch to the command prompt and just type here the command for creating new laravel project composer space create dash project space test test preferred dash list is laravel slash laravel space project name let's say project name is laravel 8 all right now press enter all right project has been created now just go inside the project so just right here cd laravel hit api now press enter now let's open uh this project inside the visual studio code so switch to the visual studio code and now just click on open folder and select project which is uh availability and now select folder now let's create a database so inside the browser just open php my admin now just click on databases and here just enter the database name let's see database all right now just click on create all right now switch to the project and let's configure this database so just open dot enb file and set this env file uh here just add the database name so you can tell the database name laravel 8 api db username report and password blank in my case all right now save this file now let's create a model so inside command prompt just type here a command for creating the model php artisan make colon model is model name let's say model image post and also create the migration so just write your dash name and now press enter model and migration both i created now switch to the project and just open the migration so just go inside the database directory in migration and from here just open create post table migration and inside this migration let's add some column here inside this schema so just right here color table data type string and column name let's say column name is type and one more column color table data type text and column name body all right now save this file now just open the post model so just go inside the app directory models and from here just open post.php file and here just write table name so just right here protected also at the level column so just right here protect dollar fillable is equal to inside the array just add here the column name so first column name is title and another body so just right here type and secure body now save this file now let's migrate this migration so this to the command prompt and just type here the command php all right now press enter migration complete you can see here inside the database just refresh database and here you can see the post table all right now let's insert some dummy record inside the post table so for that let's create a factory so for creating the factory just type here command php artisan make colon factory this factory name let's say factory name is post factory and you know center now just open this porch factory so inside the project just go inside the database directory then factories and from here just open post factory and inside this definition inside this this return array is right here column name title and arrow dollar disk arrow faker and just call here a text function and inside this let's say 50 character and body just add here the comma what semicolon arrow download this text and number of character 300 all right now save this file now just go and set the address directly and just open the database theater and inside this just right here slash app slash models and host model colon column factory and let's create 20 records so just write here 20 and just call the other create function all right now save this file now let's run this seeder so instead command prompt just type the command hp artisan vp now press enter all right database reading completed successfully you can see here just refresh the table and you can see here that in body i also created it and updated it all right now let's create a controller so inside the command from creating the controller uh just right here the command i'm just going to create a resourceful controller so for that just write your php artisan make colon controller which is controller name just let's say controller name is host controller and resource just right here all right now center controller created now let's create the resource so for that just write a command php artisan make colon resource let's space resource name let's say resource name is post resource okay now press enter okay controller created and resource created now access to the project and just go inside the app directory then http and here you can see the resources directly and inside this resources directory you can see here the post resources dot php file all right and inside the controller http controllers and here you can see the post controller and inside this host controller you can see here the methods function all right so update just drag and store and create and index all right now let's create the route for this function so just go inside the routes directory and just open up a dot php file and inside this file let's create some route so just right here rau get and uri posts all right and inside the that this array uh just right here the post controller and also at the path of the host controller like this all right and now colon colon class and just add here the function name so [Music] post controller i'm just going to create a route for this function index so just write here inside the php index all right and now uh let's create the route for the this one just close this file this migration and post factory have a seater all right and now let's create uh route for this create uh sorry this store for function all right now just copy this line and paste here method name post and uri slash post all right and make the name stored and now let's create the route for the show so just copy this and paste here method get and inside this url just add here the parameter id and method name so all right uh another route for the meter and also destroy method so for that just right just paste here the route and uri for the update uh method is put uri post slash id parameter all right and make the name of it and for the delete method delete all right and uri post and just add here the parameter id right and method name this one please try all right these are the routes now inside the host controller set this index method just right here the code for fetching all the post so for that just type here download post is equal to model name host also add the model path like this all right and now colon colon as you need and inside i'm just going to fit records in one piece 10 required so just write here and now return post resources post resource all right once also import here post resource and now colon colon collection and inside this collection just pass here in this variable post all right now inside the store method just write code for the creating new post so just right here dollar post is equal to new post and dollar post is equal to dollar request arrow title and post dollar post arrow body is equal to the request arrow body all right and inside the if dollar post just call the save function tape and here simply return new post resource and inside this just pass here this dollar post all right and for the fetching single record single post inside the show method just right here dollar post is equal to post column column find or fail and inside this just pass here this id dollar id just write here download id and simply return new post resource and inside this just pass here download all right and for the update inside this update function just right here dollar post is equal to first column column find for fill and inside this just pass your dollar id and dollar post dollar type sorry hero title is equal to the request title and the body whose dollar post body is equal to dollar request and just return new post resource and inside this just pass here dollar post all right and for deleting the post inside the display method just right here dollar post is equal to post colon colon find all fill and inside this let's pass here dollar id and inside the ifa statement dollar post arrow just call the delete function delete if record is deleted so just return here new post resources resource and inside this just pass here all right all done uh this method is used for all post records all right and for getting the new post and it will fit a single post and this update function is used for updating the post and this function for deleting the post all right now save this file all right now let's check this app here i'm just going to use for testing the api uh postman okay so switch to the post pin and before testing uh first of all run the application so just type here the command php artisan server all right now application is running now switch to the postman and uh first let's check this one which all record so just go to the url slash post so inside the postman just click on this plus icon and method get url localhost captioning not available all right now just click on send and here you can see the record okay inside this data array you can see here the post 1 2 10 all right and this is the pagination link and this is the metadata okay all right now let's uh hit single record for that just call this ura this one post just pass here the id of the post so just right here slash and i'm just going to fetch here id5 all right now just click on send request and here you can see the post teddy is fine right if i enter here the id 9 and sending the request and here you can see record the post which id is all right now let's create the new new post so for that just call this route and method is post all right so for that just open the new tab and here select the method post and you are a local host okay slash post all right and just click on body raw and inside this just select here jsn and here inside the curly bracket just pass here type all right typical colon let's try here the title name and let's say new post title and body new post body all right now just click on send and here you can see the post created and you can see here the id 21 uh let's fetch this 21 post so the title of the id 21 and now just click on send and you can see here the post all right you can also check inside the database so just refresh this table 20 records and after refreshing your table you can see here the 21 uh post new post title and new post body all right now let's check the update route uh this one all right so for that just go to the url post and just pass it the id post id and with put so inside the new tab method select put method and uri local host column 8 thousand slash api plus posts slash i'm just going to update the last post this one okay so just add here the id 21 and inside this body click on draw and then just select here jsn and inside this dsn just add here a data let's say title is colon updated post 21 and body updated post 21 description all right now just click on send request you can see uh post updated you can see inside the database table just the table and here you can see the updated post 21 updated post 21. all right let's see the uh delete cloud so for that just open the new tab and just call this route post and id and method delete so just select the method delete and uri localhost colon 8000 slash api plus posts class id i'm just going to delete this record 21 so just add here 21 and now click on send all right record deleted uh just refresh the table and here you can see there is no 21 post all right okay now let's see how can uh we customize the response okay so uh if you want to get so for that you can just customize the response inside the post to respond post resources so just open the post resources and inside this first of all let's comment this and here just right and i'm just going to return only id so just right here id arrow download this id and also return the title so just right here enter and color this arrow i'm just going i'm just not going to return created and updated okay so just write this column right now save this file and now let's check this so just add here the id now let's add is 20 because 21 is not more exist i just so now just click on send and now you can see only id title and body all right you can also add extra details with the response like you can add api version details or any url so for that just go inside the post resources and here let's create a function for adding more details let's write your public function and function name and function name is width and inside just pass your request and inside this just record an array and inside this array just right here i'm just going to pass the version so let's write your version and add your value so i'm just going to write here 1.0 and 0 all right you can add here any text here i'm just going to add here uh url let's say url going to type here list api url all right now just save this file and let's check this so inside the postman just resend the request and here you can see the extra details version and authority all right so in this way you can create crud api using resources so that's all about grad api using resources so thanks for watching this video and if you still have any kind of question you can ask me in the comment box don't forget to subscribe my channel thanks for watching
Info
Channel: Surfside Media
Views: 22,845
Rating: undefined out of 5
Keywords: How, to, Program, laravel (Software), laravel, laravel v8, laravel 8, laravel project, laravel setting up, laravel v8 setting up, laravel v8.0, v8.0, laravel 8.0, laravel 8 playlist, laravel 8 tutorial, laravel 8 lesson, intro to laravel 8, laravel tutorial, laravel 8.0 tutorial for beginners, laravel for beginners, laravel tutorial for beginners, laravel8, crud api using resource in laravel 8, crud api using resource, laravel 8 crud api, crud api in laravel 8, resource api
Id: 3Uy0KRPHQik
Channel Id: undefined
Length: 30min 25sec (1825 seconds)
Published: Sat Oct 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.