CRUD Operations in Laravel PHP Framework

Step 1 – Download Laravel 5.8

In first step you have to download Laravel 5.8 version. For this you have to go to command prompt, in which first you have go to your folder path in which you want to download Laravel 5.8. After this you have to run “composer” command, because all Laravel depository handle by composer. After run “composer” command. You have to run following command.

Above command will make crud folder and under this folder it will download Laravel 5.8.

Step 2 – Mysql Database connection Laravel 5.8

Once you have download Laravel 5.8, so now first we want to make Mysql database connection from Laravel 5.8. You can make Mysql database connection from two way.

In first way you have to find .env file in your Laravel 5.8 folder. Open that file and under you have to define your mysql database configuration like below.

In second way, you have to open config/database.php file, and under this file you can also define your mysql database configuration.

After defining above details you can make Mysql database connection from your Laravel 5.8 application.

Step 3 – Migrate Table from Laravel 5.8 to Mysql Database

In Laravel framework, you have facility from make table in Mysql database from your Laravel application. For this first you have to create migration file in your Laravel folder. For this you have to write following command in your command prompt.

php artisan make:migration create_crud_table –create=crud

This command will create migration file in database/migrations folder. In this file we have to define table column which we want to create in table. Below you can find migration file in which we have define table column.

Now we want to migrate this table definition from this Laravel application to mysql database. For this we have write following command in command prompt. This command will make crud table in mysql database for perform CRUD operation from Laravel 5.8 application.

php artisan migrate

Step 4 – Create Model file in Laravel 5.8

In this we will seen how can we make model file in Laravel 5.8. This class file mainly used for do database related operation in controller class. For create model files we have to write following command in command prompt.

php artisan make:model Crud -m

This command will make Crud.php model file in app folder. In this file we have to define table column name which you can see below source code of Crud.php file.

Step 5 – Create Controllers in Laravel 5.8

Controllers files mainly used for handle http request in Laravel 5.8 application. Here we want to create Laravel 5.8 crud controller. For this we have to go to command prompt and under this we have write following command.

Step 6 – Set Route in Laravel 5.8

This is very useful step in Laravel 5.8, because here we want to set route of all CrudsController class method. For this we have to open to routes/web.php file. In this file we have to write following code for set route of all method.

Step 7 – Set Data in View File in Laravel 5.8

This is the last step in Laravel 5.8 Crud application, and in this step we have to set data in view file which has been store under resources/views folder, because this view file has received data from controller method, so here we have to set data in view file. Below you can file all view file which has been used in Crud application, and you can also find how data has been set and how to make form in Laravel 5.8 view file.

resources/views/parent.blade.php

This command will make CrudsController.php file in app/Http/Controllers folder. Once you have open this file, then you can find all predefine method for do CRUD operation in this controller file. We have to just add code for do particular operation. Below you can find CRUD controller file code below.

Controller index()

Paste the following code into index() function.

index() – In Laravel Crud controller index() method is a root method of crud controller, so when in browser we have enter base url with controller name, then it will called this index() method. Under this method we will write code for display data from mysql database. In this code it will first fetch data from crud table and store under $data variable, after this we want to make paginate link by using paginate() method in Laravel 5.8. For send data to view file, for this we have use view() method for send data to view file. Source code you can find under index() method.

resources/views/index.blade.php

Controller Create()

Now copy the below code and paste into create() function.

create() – This method has been used for load create.blade.php file. In this file user can find form for insert new records and filling data insert data request will be send to store() method of CrudsController.php controller class.

resources/views/create.blade.php

Controller store()

store() – This method has received add or insert new records request received from create() method. In this method, it will perform two operation. One is for upload image file by using move() method and second is for insert records into mysql table by using model class. Before upload of image, it will rename image name. After successfully insert or add of data in mysql table from this Laravel 5.8 application, page will redirect to index() method with success message.

To insert data into the database, paste the following code into store function.

Controller show()

To insert data into the database, paste the following code into show function.

show() – This method in Crud controller has been used for fetch single data details based on on value of $id argument. For this it has been used findOrFail() method. After fetch details it will send to view.blade.php file.

resources/views/view.blade.php

 Controller edit()

To insert data into the database, paste the following code into edit function.

edit() – This method main function is fetch single data from Mysql database and load into edit or update form for make required changes.

resources/views/edit.blade.php

 Controller update()

update() – Update method has received edit or update data request from edit(). This method has done two function like upload of profile image with update or edit mysql data in Laravel 5.8 framework.

To insert data into the database, paste the following code into update function.

 Controller delete()

delete() – Delete() method mainly used for remove single or multiple data from Mysql Database. This is last operation Crud Operation in Laravel 5.8 Crud tutorial series.

To insert data into the database, paste the following code into delete function.

Step 8 – Run Laravel 5.8 Application

Lastly, we want to run Laravel 5.8 Crud application, for this we have to go to command prompt, and write following command.

Dharmendra K
Latest posts by Dharmendra K (see all)
Total Page Visits: 123 - Today Page Visits: 1