Laravel 8 Crash Course Part 12: Laravel 8 CRUD using Resource Controller | Insert Read Update Delete

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back so guys in this video we are going to learn about laravel 8 crud application with resource controller so guys we will be using resource controller to make a thread application so guys for demo we are going to start with the posting of blogs or post things okay so let's let's get started with it so first let's move to our editor and let's open our terminal so control plus still and let's create a model and migration at once so let's type php artisan make colon model where my model is post hyphen m so this will create the model and migration table both at once so it's now as simple let me just minimize this again let's go to our database first let's check the table migration over here you can see post table perfect so now as we saw in previous video we have done with the login things so after that login we need to store the user id while posting this data so let's get that integer of user underscore id and dollar table let's give string and the title will be title and let's give medium text let's give this also because sometimes title is too big that time it will be useful so title and description so this is will be the description and then dollar table one status thing we are going to give tiny int tiny integer which will be status and by default it should accept zero and also will allow nullable let's save yep guys so let's migrate this into a database php artisan migrate so it's migrated perfectly and now guys let's close this and let's move to our model model called post just now we created so let's configure the table with the protected dollar table where my table name is posts a simple let's see in our db refresh our database and you can see that post table is there so let's click on this you can find all the things let's get back and now let's call all the column fields so protected dollar fillable is equal to we missed out protected so first is user underscore id then second is title t-i-t-l-e and third is the description so let's copy that description spelling and last is status just leave that save it and let's close this guys perfect so now main we are going to create the resource controller php artisan make colon controller the resource controller so post controller hyphen hyphen re s4u rce hit enter your controller created successfully so now let's go to app http controller post controller at the last cool so as you can see all the functions have been created automatically which is for crowd applications required okay so let's save and let's close this terminal so let's first we need to create one folder inside our resource views let's create a folder with the name blog and inside this we need one index file index.blade.php now extend the section so we will copy this our default home section so we are extending that front-end layout and inside this we required few things we are going to keep that and others we are going to remove it as simple so this is my card header card body and over here posts let's add a tag add posts add a class button oops button button primary floaty float and and let's create the route url for the posts forward slash create so this is default thing let's go to web.php and create a resource controller a resource resource where i want to tell posts okay post at the post controller so what is our controller name let's copy this and directly paste that so let's remove put single quotes and paste it done so let's check the route list now so when you check with the route list all the things you will understand how this is working so guys now let us open a route list php artisan rvt route list hit enter you can get the post controller over here you can see post controller so when you create this you can see this this is the route which is using get method perfect so let's minimize this and let's continue to create our card as simple so guys now let us view this page over here let's come here we need a button for that calling posts okay so let's take this posts and go to our navbar layouts inc inside our navbar i need one button so let's add that posts and paste that posts save refresh perfect so now guys once we click on this it should go to our post controller at index function over here let's return that return view where you are going inside the blog folder at index page done and now it should work so let's click on post you can see on the post you are there at add post okay so now let's click on this add post you can see post you need to give posts so make sure you are giving correct spelling let's go back to our index page over here you give posts because we have given the route as posts posts done and yep guys so let's move to our resource views blog let's create a file called create.blade file to insert the data so let's copy the same design and let's keep this in h4 tag add post and for this we will tell back button change the button design as simple let's save and let's see the output guys for this let's see refresh the page let's click on add post you can see create post so let's go to the create function inside our post controller so you can see that create function nothing is there let's return return view inside my blog folder create file done and now you can check the page will load code so let's now let's create the form for that let's get back to our create file let me close this terminal first let us create a form and give the method is equal to post and create a url with the posts post forward slash what is the next procedure to store that let us see in the terminal in our route list so you can see to store that you are getting this same post method okay so you just need to send only posts nothing else so let's leave this like this only and create your input fields so form hyphen group dot margin bottom three input dot form control so guys now let me just design this form and show you the output finally i have designed this form and now let us see the output guys for this let's get back refresh and now when we fill inside this it should store the data once we submit inside this table so now let us write the code to submit that guys so first let us go to our post where the url is going to a controller at store so let's create one posts variable is equal to post and then let's call this with the first column name which is user id so let's call this user id is equal to authenticated id okay so authenticated user id so why i want this because i want to store the data of user id for this post who have posted this so first guys you need to import the class of this let us import that so you can see that post is imported and we are using authenticated user id so let's import this class also at the top which is this okay as simple and now guys to check that when you insert this record that time you need to check that right is it authenticated or not so for that let's go to our route and set the middleware for it so let's create one route middleware and middleware group middleware let's keep this authenticated auth1 so auth middleware is created by default so let's include this inside this save and now let's try to access this posts let's get back let's click on this posts you can see it's redirecting okay okay we are already logged in okay so that's why it's inside now let us try to click on post you will be redirected login thing okay so let's way that gmail.com123 four five six seven eight nine let's hit enter you are in inside the post cool so now let us add this guys so let's get back let's go to a controller and dollar post with the title is equal to dollar request of your input field so what is my input field name attribute value you can see in create title copy this and paste so let's copy this and make that description also let's copy this description and paste let's paste for the status status let's copy the status is equal to double equal to true with the colon when it is set to true when it is checked it will be one if it is not checked it will be zero as simple and now let us save this guys save function and return and redirect back to same page where the post was added with the message i want to show at the status where post added successfully save and now to show the message or your status of that let's go to create page so now let's use if condition session of your status and if if it is success then you have to show that in success alert let's view that paste oops let me just yes session function of status save guys so right now let us test it let's get back refresh and now let's try to insert so this is first post copy paste until description description let's keep it unchecked only and submit let's click submit page expired why because we didn't pass the csrf token so let's add csrf token because form action requires the token let's get back let's refresh again and add this paste let's submit cool so post added successfully why that alert design didn't come let us check in that lart yes okay this was there save this and let us add one more detail this is second post copy paste description let's submit cool so post added successfully let's refresh you can see everything is working fine cool so now guys let's get back on the post you are on the post let's see the database you can see that data is inserted with the user id one perfect and now let us view that get back let's go to index so first increase the column to 12 and let's create a simple table over here table dot table dot table bordered dot table striped table head table row table heading id title and the show let's show the status and the edit button as simple and then one delete button is there and let's open the table body let's let me close this route table row table data so first you need to loop that so how will you look let's go to a post controller at the index function let's create a variable called post is equal to your post model where i am going to get a all function as simple let's use a compact function to pass this variable so let's set this and this variable will be pushed inside your index let's go over there use a for each loop oops for each and the post variable as simple let's cut this and paste over here and let's take this item of inside the open curly and close curly brace item of id first and then which user have posted this you can take that user id user underscore id let's mention that user save and then title and the status so status we are not going to check in this method we will check in another format so let's use if condition if status double equal to one then you will tell hidden else you'll tell visible and if save and then simple thing another for edit and delete button edit let's add a class for this button button small button primary so let's copy the same thing and paste again for the delete as danger and let's set a route for this url which will be posts forward slash edit as simple so let's save and let's see the output guys for this first refresh you can see that table has come and data has come so let's now let us do the relationship inside this let's do the relationship let's get back let's go to our post controller i mean model app model let's go to a post model public function users function public function function name users where return dollar this belongs to which to user model so let's call user colon colon class comma in single quotes then give comma and single quotes so user model id and this post model foreign key id as simple save that's it so now let us see the output for this so before seeing the output you need to call that so let's this is the user id so paste that at that place and give name so this name is nothing but user table column field so i'll show you that user table this column name okay so i'm copying that and pasting here save let's refresh perfect so you can see that this post is posted by ved prakash and yup so now let us log out login gmail.com one two three four five six seven eight let's login and let's go to post with the logged in by um so home is logged in now and let's add a post this is third post description let's submit you can see post added successfully let's go back you can see that um is this is third post added by oh cool so now guys let's try to edit this so let's move to editor let's close this create file web route post model let's check the route list guys first over here this is my post controller and to edit the data so guys let's take this edit one and so let's copy this copy and concatenate over here dot dot paste so this will call your post id with the edit so this will call your function with the edit okay so let's go to edit just ignore with the show now we are going to work with edit so let's return return view inside my blog folder blog dot edit file so let's create that edit file first edit dot blade dot php and we need the form design same as our add the post so let's copy and get back over here let's close this create file and we are working on edit file let's change the heading to edit yep guys so let's add the values inside this value which will be dollar post so how are you getting this dollar post first so let's take this variable dollar post and go back to your controller at edit function so over here you are going to create that dollar post variable is equal to call your post model colon colon with the find function pass the id dollar id so this dollar id is passed now simple once it's passed you are going to take this variable and send to this edit file using compact function so let's send over here and let's go to edit file with this same post variable as simple so let's take this and paste inside the text area so if in case anyone is using html tags to store in description you can use this symbol now let us use this condition same thing to check if the status is checked or not so you can paste over here and let's call if it is equal to 1 then you need to show checked else it should be not as simple or else you can just directly give this one also save and now let us change this submit button and update this form action url so how will you update that let us see the route list first so to update we are going to use put or patch method so let us use the put method and just give post with the id as simple that will call your update function at the update perfect let's use at method which will be the put and pass the id inside this dot dollar post of your id simple done so now let us refresh and check the first output guys and then update the data let's get back refresh let's click on this first id edit you can see id number one with the edit this is first post first post and it's not checked let's say with the other id you can see third post third was not checked so let's now let us click select this and let's still update it and let's update the data now so when you click on update that time what happens it goes to the post controller with the update function so let's write the code to update that so first create a variable called post is equal to your model post find a function with this id let's find this id and then as simple same what you have copied for store so let us copy the same thing all those things and make few changes paste we are going to store the user id and the status also on this post method you have to give update function as simple and then return and redirect back to same edit page and tell the message as updated successfully let's save and now let us see the output guys so let's get back refresh let's tell updated and check this status and let's click on update perfect post updated successfully let's get back to post and see that visibility so you can see own posted data third post is hidden the status also has working fine cool so guys now let us try to delete this post using our resource controller let's get back so this is a final step let's move to our index page of the blog and here guys so let's see the route first as you can see for delete you are giving this id it's not mentioned that you are going to use get out post but it's mentioned as delete so let us call this by fill form let's minimize this let's comment and add the form tag give the path url function where posts forward slash the id as same as this let's pass this and the method is equal to post pass the csr of token and then the method will nothing but delete as shown in route and then a simple one submit button let's copy the same thing and paste over here and change this a tag to button done and make this type to submit and now let us get back let's refresh let us try to delete this click to delete okay post method is sent but we didn't write the code let's get back let's move to a post controller let's write the code so first create a variable post is equal to post model where i'm going to find the id first so let's take this id and paste and after this you need to call the delete function delete function and same way return and redirect back to the same page with the message called deleted successfully so to show this message on the index page so on this page let's see the code for that let's copy this code and paste inside the index page save it and now let's see the output guys refresh and now let's click to delete again cool so post deleted successfully let's try to add so dummy dummy details let's submit post added let's go to post and let's again delete this cool so you can see that guys our data is deleted from database using resource controller so perfectly everything is set guys so in this video we have covered with crowder application of post so guys in this video that's it and let's continue next video with the new topic so thank you for watching this video guys please subscribe like and share
Info
Channel: Funda Of Web IT
Views: 2,987
Rating: undefined out of 5
Keywords: fundaofwebit, crud using resource in laravel, laravel crud generator, insert data using resource controller, laravel 8 crud using resource controller, resouce controller in laravel, how to make laravel 8 crud in resource controller, fetch data using resource controller in laravel 8, crud operation using resouce controller in laravel, crud in laravel 8 using resouce controller, resource controlle in laravel 8, laravel crud tutorial using resource controller, laravel 8 tutorials
Id: Q9apXcbdgY8
Channel Id: undefined
Length: 30min 37sec (1837 seconds)
Published: Fri May 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.