Laravel CRUD with Resource Controller and Route Model Binding

CRUD — Create, Read, Update and Delete is essential functionality for almost every web application. With help of laravel, this can be easily done through some functionalities like resource controller and route model binding that ships with laravel out of the box. In this blog, we are going to perform simple crud operation for user module. This blog is mainly written for laravel beginners to kickstart their laravel journey.

Installing Laravel

First things first. Let us install fresh laravel application by the following.

With Laravel Installer

laravel new crud

With Composer

composer create-project — prefer-dist laravel/laravel crud

Once that is done, configure your .env file for database connectivity and then boot our application with

php artisan serve

Now you should be seeing laravel home page.

laravel crud resource controller route model binding 1

Migration

Lets create database tables by migrating default migration files that ships with laravel

php artisan migrate

Basic Authentication

We are going to use laravel’s inbuilt authentication system so that we will get nice bootstrap template setup.

php artisan make:auth

To know more about how authentication system works, check out authentication quickstart

Now, your homepage will look like this.

laravel crud resource controller route model binding 2

You can go ahead and create a user for yourself to login into the system.

HTML Collective

In this tutorial, we are going to use laravel html collective for easier form handling. You can checkout its documentation here and install the package.

Create Controller

First, we need to create a resource controller.

php artisan make:controller UserController — resource

Above command will generate UserController along with index, create, store, show, edit, update and destroy functions like the following

If you give only controller name and not — resource then you will get plain controller file without any functions.

Quick Tip:

If you want to create a resource controller inside a folder say, inside Admin folder, you can achieve this by specifying the folder name before the controller name. For example like this

php artisan make:controller Admin/UserController — resource

Routes

Now that we have user resource controller setup, lets add our route configuration. As mentioned earlier, we are going to use laravel’s resource controller and route model binding.

Route::resource(‘users’, ‘UserController’);

If you never came across laravel resource controller, checkout laravel documentation here

Route Model Binding

Laravel provides a convenient way to inject model instance to routes that are binded with model. This can be done in two ways.

  • Implicit Binding
  • Explicit Binding

But we are going to see only explicit binding here. I may cover an article about implicit binding in coming days. You can also learn it through laravel documentation

For explicit binding, look for app\Providers\RouteServiceProvider file. Inside the boot function add the following line

$router->model(‘users’, ‘\App\User’);

Menu

First, we need to add user module to navigation menu. So, lets do that by adding the following code to resources/views/layouts/app.blade.php in menubar navigation.

<li><a href=”{{ url(‘/users’) }}”>Users</a></li>

User Listing

Next, when we navigate to localhost:8000/users url, you will see an empty page. This is because we have not configured our controller yet. Lets configure our user listing page by creating users folder under resources/views folder and inside it create all.blade.php with following code

Once our view is ready, lets modify the index function of UserController.php like the following

here latest() function will return user collection with order by updated_at field. However, you can override it by passing the column name like latest(‘id’). This will return user collection with order by id desc.

laravel crud resource controller route model binding 3

Now your localhost:8000/users will look like above.

CREATE

Now, let us create an user by clicking on the add user button near user listing. You will see an empty page. Because we have not called necessary view files yet. Lets do that by creating a create.blade.php file under resources/views/users and add the following code

In above code, we have included _form.blade.php partial. I have done this, so that we can maintain same form for both create and update. Lets create _form view and fill with following code.

Now, that we have view file setup, lets modify our create function in UserController.php

Now when you hit localhost:8000/users/create, you should see something like this.

laravel crud resource controller route model binding 4

Filling up the form and submitting it will hit store() function. Lets add validation and logic to save our new user. Also, here I’m going to extract our validation rules and new user creation to new function so that we can avoid code duplication.

You can of course use Form Request Validation for validating form. But for article purpose I have kept it simple.

Once user is created, you will see the following.

laravel crud resource controller route model binding 5

READ

Now, that we have completed user creation. Lets handle read logic. Since we have used route model binding, for show function in UserController, we will be getting user instance. So we can create view.blade.php file and pass in the user instance to display user details.

view()

Laravel View User

UPDATE

For update, we need to create update.blade.php file with the following content

Laravel Edit User

Then modify our edit() function

Once we submit update form, we will hit update() function. But here, we are going to have same code similar to store() function except we will pass in our user array.

After updating, you will see something like this.

Laravel User Updated

DELETE

When user hits delete button, destroy() function will be accessed. So lets modify that.

When a user is successfully deleted, you will see this.

Laravel User Delete

That’s it! An easy way to process crud operations in laravel with resource controller and route model binding.
 
Happy Coding!