05. Controllers - Laravel 11 tutorial for beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends Tony here welcome so in the previous video we worked with the routes so if I open the web routes here we have what we have done in the previous video we have the route for the for slash when we navigate the first page then we make a post request a put delete and also to get a test when we navigate the SLS but in real world we don't use uh anonymous functions because for example here we make a post request for example to create a new post or a user and when we create a new user we are first going to yeah here for example create new post but before we creating a post also you need to make up the logic to validate validate the data and then create new post and this may be uh 12 or 20 lines the same thing for the put request and the same thing for delete for example and if if we have more than just a post CR uh this file may be uh too large for that we use controllers and in laravel a controller acts as the middleman between incoming requests and the applications logic it's a class that groups functions also called uh methods and that handled this request for instance a controller named user controller might handle all requests related to user such as creating a new user uh updating a new user deleting a new user and we use controllers also for better organization of your code by separating request and logic from routes and this makes your code more maintainable and easier to understand and we have some commands to create controllers let me open the terminal and if I say PHP Artisan list hit enter and here we have all the lists what we can do with this PHP Artisan so and we have a make controller which is going to create a new controller class okay so let's clean and let's say PHP artisan make colum controller and hit enter and we have a prompt here what should the controller be name and let's say for example I'm going to make a post controller now I'm going to name this post controller you can name post plural but I prefer to use singular here and also I want to add a controller at the end because we're going to use so with the models and I'm going to create a post model so I don't have I don't want to have a conflict with name so let's specify this this is a post controller and hit enter and now which type of controller would you like we have empty to be an empty class resource this is going to have all the functions or methods we need to make a resource so a gr for post for example then we have a single tone API and and invocable so let's start with empty here and as you can see now controller inside and here is the path app HTTP controllers post controller.php created successfully let's see let's open the directory up hasp controllers and here we have the post controller and we have an empty class which extends the controller okay let's delete this and because we are going to make a crft for control for post and let's create a new controller but now let's name it post controller again and let's choose a resource in this case hit enter and what model should this resource controller be for this is optional so I'm going to skip this okay now the controller created let's close and here we have post controller now if I open this as we can see we have some methods here so the index method and also here we have display a listing of the resource for example post show the form for creating a new resource here we have a create method a store method store a newly created resource in storage in database then we have a show to display the specific resource so going to work to display a single post edit show the form for editing the specified resource uh update to update the specified resource in storage and then we have a destroy now as we can see we have here on the update also on the edit and on the show injected here string dollar sign ID and we need that ID to find the post for example and show display that specified post but if I open and let's delete this post controller just keep in mind this in show we have string dollar sign ID string dollar sign ID and also here string doar sign string doar sign ID but if I delete and let's open the terminal let's clean and let's run for the last time the make controller let's let's make post controller hit enter say resource but now what model should this resource controller before this is optional we ski in the previous one now let's say the model is post we don't have the model but let's say for now model is going to be post if I hit enter and we have a prompt so the model does not exist do you want to generate it yeah and now we have generated also the model for this post but what I wanted to show you is if I open now the post controller yeah we have the methods but on the show now we don't have the string dollar sign ID but we have the injected the post model so is binding the post directly not only the ID so we don't need to find that post is directly pass it here and also for the edit and update and Destroy okay now let's use this controller to clean the route here okay so here we have the post and this is going to be for example for the SL post and instead of using this Anonymous function we can use now the controller we created so inside the array we to add an array inside the array we going to add first the controller name so post controller and make sure you import that post controller up here and col col class and the second is going to be in single quotes the name of the method so for post we want to create a new post and if we go here here we have the store which is store a newly created resource in storage so for post we're going to use the store method and in single quot we add the name of the method store here we have a put which is going to be slash post slash ID and instead of this function also here we're going to use the controller class and the name of the method is going to be update then we have a delete and this is going to be a controller class and the method the name of the method is going to be destroy if you see here we have the destroy method then let's remove that here we need to specify this is going to be pause ID to delete we have a post put and delete but we don't have a get to get all the post so let's create that route get/ post and and we use the PO controller class and then and the method name is index then what we need is if I also see here first we have the index and we have the create we need a new route for the create method so let's say route get post create and using the post controller class and the create method and then we have a post then we need to show a single post so let's after we make a store we want to show a single post so let's say get post okay we use a get method here post pass in here the ID because on the show method we going to receive that post so larel is smart enough instead of just passing the ID and then find it here we are going just bind the the model I'm going to show you in the next video don't worry so we have post then after the show we have the edit so we need also one more for the edit after the show and here we have a get method which is going to make a get request a post passing also here the ID and then the edit we just going to return a view with a form to edit the post and now we are okay we have seven routes and also on the post controller we have seven control seven methods here so the index one create je store three show four edit five update six and Destroy s okay now if I save this and let's open the terminal I'm going to clean and I'm going to use a pH P artisan and instead of list I'm going just specify I want a list of the routes so route colon list hit enter and here we have the first route which is get then we have a post and get the ignition ignore for now then we have get puse as you can see here and then here we have the controller with that method okay post controller at index then uh post post request post and for that we have a post controller at store and so on as you can see here we have total 12 routes good but because we are going to make a resource for the Post instead of something like this we can use just the route colon colon resource and here we specify the URL which is going to be /st and here we just say in post controller not specifying the methods or something else just post controller and that's it so let me comment this one now and this is the same as what we have added manually 1 by one here let's save and let's open the terminal again okay so here we have 12 routes if I clean and run again that route list hit enter as you can see again we have 12 routes but now you can see on the post uh post routes we also have the names so you can see post Index this is the name of the route okay so what do mean if I uncomment this and let's comment the rou resource here if we want we can add the name for the route and for example post index I'm going to name this and let's open Terminal and run again and as you can see now the get post has the post index name because we added manually but instead of doing all of them manually let's remove and let's use this route resource okay so now we have a clean web PHP file okay and also here when we make a get request slash and or use this Anonymous function here we just have return a view now if you want only the return a view because in the real world we make a logic and for that we use controllers but if you want only to return a view instead of this we can use a route colon colon view okay and now here we specify the path in this case it's going to be just slash and here the name of the view which in this case is going to be welcome okay so this is the same as this one but is more clean so let's remove that and now we have very clean web PHP file we created also the controller which is going to be the logic for or post resource okay friends that's it all about this video what I wanted to show you now if you like such a videos don't forget to subscribe to my channel like the video share with your friends and see you in the next one all the best thank you very much
Info
Channel: Code With Tony
Views: 1,804
Rating: undefined out of 5
Keywords: laravel, vuejs, livewire, laravel 8, laravel 9, laravel crud, laravel 8 tutorial, laravel 9 tutorial, laravel 10, laravel filament, laravel 11, laravel 11 full tutorial, laravel beginners tutorial, laravel for beginners, learn laravel, laravel folder structure, laravel vuejs, laravel reactjs, laravel routes, get route
Id: CVdbtELyTl8
Channel Id: undefined
Length: 14min 47sec (887 seconds)
Published: Sat Mar 23 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.