Crud is simply the basis of any web/app development features. All we have to do on any web page is Creating, Reading, Deleting, or Updating the content and that is known as the acronym CRUD. In this blog, we learn about how we perform CRUD operation in Laravel 5.8(PHP framework).
Step 0: Prerequisite
- Install xampp a PHP development environment. (Version 7.4 best suited for 5.8 Laravel)
- Install composer(to install libraries of Php for any application).
- Install VS code(Code-editor).
Step 1 : Download/install Laravel 5.8
Go to command prompt and then go to the xampp folder in C drive by cmd. [C:\xampp\htdocs\]
In cmd Run :
composer create-project laravel/laravel=5.8 crud --prefer-dist
It will install laravel 5.8 in your xampp/htdocs folder, and the folder name in which the Laravel is installed is “crud”(You can change this name of the folder while writing the command in cmd).
Step 2 : Create the Database
For the connection of the database first we have to run “xampp control pannel”.
Then in action tag start the Apache and MySQL.
and then click on Admin of MySQl. It will redirect to http://localhost/phpmyadmin.
- Go to the database.
- Give the name of the database and click create.
Step 3: Connection to the database.
In the .env file of the Laravel folder(named crud in this blog) open it with VS Code. Change your code to this.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=gurukul
DB_USERNAME=root
DB_PASSWORD=
where database name should be the same as you created in step2. and change DB_USERNAME to root.
Step 4 : Migration of the table(From Laravel to MySQL Database)
We can make table in MySQL using the Laravel. For this we have to run this following command in cmd.
php artisan make:migration create_crud_table –-create=crud
You can use any name of your table instead of create_crud_table. The above command is going to create a migration(PHP file by the name of the table) file in the database/migration folder.
Edit the code and paste this :
Now in the command prompt use the following command :
php artisan migrate
Step 5: Create a Model File in Laravel
php artisan make:model Crud -m
In cmd, use this command to create the model file(You can use any name instead of Crud while making other projects)
It will add a Crud.php file in the app folder and in this file in a variable define the table column.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Crud extends Model
{
//
protected $fillable = [
'first_name', 'last_name', 'image'
];
}
Step 6 : Creating the controller
Controller is used to handling the http request. They are stored in app/http/Controllers directory.
We have to create the controller using the command line:
php artisan make:model Crud -m
Note : For making CrudController file in controllers
php artisan make:controller CrudsController
Step 7 : Setting the Route
Setting the route of all the CrudController class method in route/web.php
Route::get('/', function () {
return view('welcome');
});
Route::resource('crud', 'CrudController');
This is very important method.