In this video, learn how to use the Laravel Artisan console to build standard routes for a controller for a basic CRUD or create, read, update, and delete controller.
- [Instructor] Laravel has what we commonly call resource controllers. These are controllers that expose the most common actions that you might want to take against a resource, which typically corresponds to a database table. These controllers have corresponding routes that match these common actions. Those common actions are in programmer parlance typically called crud or bread. Or the ability to index or browse all the records, create or add a single record, read a single record, update or edit a single record, and finally to delete a single record. Let's start with the traditional manner for Laravel, which is creating matching routes for a new resource controller for managing reservations for our hotel. We'll open up in our project in the routes, web.php file. We'll start off building the routes the long way, so you can see exactly which routes we're building. We'll add after our route, get rooms, right on line 26, route, colon, colon, get, parentheses, and in single quotes forward slash bookings. So this is going to give us the ability to manage bookings for our hotel. We'll then pass in another string to our route::get function. This is goin to define the controller, and corresponding controller action for the route. In this case we'll do BookingController @index. This says we have a route forward slash bookings that accepts get requests and routes our request to the booking controller in the index action in that class. Laravel uses a seperate route and a separate action for each corresponding side of a form. One, a get to show the form, and a corresponding post, put or patch for submitting the form. And these are separate controller actions. We'll now add a Route, colon, colon, get, parentheses, and then in single quotes, forward slash bookings forward slash create. This'll correspond to our controller action bookingController @Create. Which will display the create form for our bookings. We'll add another line on line 28. Route colon colon post to post our bookings create form at, in parentheses, and then in single quotes forward slash bookings, comma, our BookingController @store which will store the responses to our create form. Notice here we can route to the same URL but because it's a different HTTP method Laravel will know the correct corresponding action to route to in our controller. Now we're going to see something new. Laravel has the ability to do something called route model binding. Route model binding is a super awesome feature of Laravel. Whereby we can tell the route that it matches a particular model, and thus it will use the id passed in the URL to check if it matches a record in the database. And if it doesn't, it'll automatically throw a 404 not found exception on that route. To see this, we're going to build a view action route which is going to be on line 29 with route colon colon get parentheses, single quotes again, forward slash bookings, forward slash, and then in curly braces, we'll pass in booking, which is going to be the name of the model for our booking records in our database. We'll then pass in the controller and the corresponding action which will be [email protected] show. You'll see we pass the name of the model in parameters. These parameters are used to id the different inputs in our controller actions. If we swap this with id, we would then have a generic id passed into our controller. There are ways of making more complex routes that multiple bound models, or have multiple parameters bound. And even adding constraints to what can be passed in the URL. Including providing for optional parameters, but we're not going to cover that in this course. We now want to create a method for viewing the form for editing and booking. On line 30, this will read as route colon colon, get, in parentheses, single quotes, forward slash bookings, forward slash, and then in curly braces, booking, to define which booking we're going to be editing, and then forward slash edit. This will then again take our corresponding controller BookingController and the action that it will be corresponding to is the edit action. We now need to submit our form, and we'll do that with Route colon colon put, parentheses again, single quotes again, forward slash bookings, forward slash, in curly braces, booking, then we'll pass in our controller, [email protected] Update. Put and patch are two similar http methods. The put is defined as putting a replacement resource. As in you swap the whole resource with this new record. Where as the patch is typically defined as how we'll think of it from in English, where we patch, or make a few changes to an existing record. In the concept of a web request it may not matter that much whether it's put or patch, but for an API it may make sense to choose one method over another. The last thing we need is a route delete method. We'll do this by adding on line 32 route, colon, colon, delete, parentheses, single quotes, forward slash, bookings, forward slash, pass in our route model parameter inside curly braces with booking, and then we'll have it talk to our BookingController, and the action will be destroy. Now we can see if any of these requests actually work. Let's make sure your homestead box is booted, and then if we go to our browser, and attempt to visit homestead.test/bookings we'll get a message saying there's no corresponding booking controller in existence. What about if we try a request to visit a non existent booking? Say homestead.test/bookings/1 same thing about no matching booking controller. Okay, now we know at least that the routes exist. But there's an even easier way to create routes in Laravel for a resource controller. Let's go back to our text editor and go back to our routes, web.php file. Let's erase all of lines 26 through 32. And we're going to replace it with a single line. Route colon colon resource in parentheses bookings comma, in single quotes, BookingController. This says to create a matching set of resource routes for BookingController and uses the model booking. If you go back to our browser and refresh, you'll see our Laravel application is still attempting to find a corresponding BookingController. So that is how to make some pretty simple routes in Laravel, and even better how to quickly generate the standard routes for a standard crud controller in Laravel.
Updated
10/4/2019Released
7/8/2019- Exploring Laravel
- Setting up simple authentication
- Controllers and routes
- Create and index view
- Show, edit, and destroy views
- Models
- Writing scopes in Laravel Eloquent
- Creating one-to-one relationships
Share this video
Embed this video
Video: Resource controller routes