Javatpoint Logo
Javatpoint Logo

Laravel crud

In this topic, we will learn how to create the laravel crud in laravel 5.8.

The following are the steps required to build the crud app:

  • First, we create a project named as 'crud' in laravel 5.8.
Laravel crud

Laravel crud

The above screenshot shows that the 'crud' project has been created successfully.

  • Now, we create the database in phpMyAdmin.
Laravel crud

In the above screenshot, we have provided the database name as laravel_crud.

  • Our application will work with a laravel_crud database. Edit the .env file.
Laravel crud

The above screenshot shows that we have modified the .env file. We have provided database name as laravel_crud to the DB_Database field, root to the DB_Username. We left blank to the password field.

  • Now, we create the migration to create a table in a laravel_crud database shown in the below screenshot:
Laravel crud

The above-highlighted statement creates a migration "create_user_table", and the name of the table is 'user'.

  • Open the migration file that you created in the above step.

We have created four new columns (first name, last name, gender, qualifications) in the user table shown in the above code.

  • To migrate the above changes in a laravel_crud database, we use the command given below:

php artisan migrate;

Laravel crud

After migration, look at the database given in the below screenshot:

Laravel crud

The above screen shows that the user table has been created under the laravel_crud database.

  • Now, we need to create a model to perform a database operations.

The above screen shows that the 'Crud' model has been created successfully.

  • After the creation of a model, we will move to the app folder where the crud model is created.

Crud.php

In the above model, we have provided two attributes, i.e., $table and $fillable. The $table is an attribute that contains the name of the table, which Crud model is going to use while the $fillable attribute contains the array of the names of the columns which cannot be NULL.

  • Now, we create a controller with a resource that implements all the CRUD operations.
Laravel crud

The above screenshot shows that the CrudsController has been created successfully.

The structure of CrudsController is given below:

The CrudsController contains the inbuilt functions (index(), create(), store(), show(), edit(), update(), destroy()).

Now, we implement the CRUD operations through the methods available in the CrudsController.

Insert Operation

  • First, we create the route in the web.php file that performs the insert operation.
  • Now, we create a view named as create.blade.php in resources/views directory.

Output of the above code would be:

Laravel crud
  • The above code calls the store function of the CrudsController class, and the code of the store() function is given below:

Suppose we enter some data in the form, and then click on the Insert button shown in the below screenshot:

Laravel crud
  • Let's look at the database:
Laravel crud

The above screenshot shows that the data we entered in the form has been successfully saved in the database.

Retrieving the records

  • First, we create a route in the web.php file.

Route::get('/show','CrudsController@index');

The above statement creates a route with a url '/show' which calls the index() method of the CrudsController class.

  • The above route calls the index function of the CrudsController, and the code of the index() method is given below:

In the above code, we use the all() method that retrieves all the records of a table associated with a Crud model, and stores it in the $cruds object. We pass the $cruds object to the index.blade.php file by using the view() method.

  • The code of the index.blade.php file is given below:

Output of the above code would be:

Laravel crud

Update operation

When we click on the Edit button, then it calls the edit() function of the CrudsController class. The code of the edit() method is given below:

CrudsController.php

In the above code, we use find() method that finds the record of a given id, and stores it in the $crud object. We pass the crud object to the edit.blade.php file.

edit.blade.php

After clicking on the Edit button, the screen appears which is shown below, and it asks you to update the data.

Laravel crud

In the above screen, you can change any field according to your requirement. Suppose I entered 'Harshita' in first name, tripathi in last name, and other fields remain same, click on the Update button as shown below:

Laravel crud

After clicking on the Update button, the control moves to the update() function of the CrudsController.php file.

CrudsController.php

In the above code, we use find() method that finds the record of a given id, and stores it in the $crud object. We pass the crud object to the edit.blade.php file.

edit.blade.php

The above code updates the database.

Let's look at the database:

Laravel crud

The above screenshot shows that the data has been updated successfully.

Delete operation

If we click on the delete button, then it calls the destroy() function of the CrudsController class. The code of the destroy() method is given below:


Next TopicLaravel Validation





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA