CRUD OPERATIONS IN LARAVEL PHP FRAMEWORK

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 :

update the function up()

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.

Tagged : / /

CRUD operation with the help of Laravel Framework.

Basic requirements:-

  • Steps for installing Laravel Framework
  1. Open Command Prompt and type
composer create-project --prefer-dist laravel/laravel project_name "5.8.*"

in place of project_name you can choose any name.

2. once Installed finished type

composer create -project laravel/laravel project_name “version_name.*”

3. Mysql Database connection

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

you can define your own database which you have created in PHP my Admin page.

5. Migrate Table from Laravel to Mysql Database through terminal

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.

In Database / migration folder we make our schema.

6. Once schema is made we have to migrate our schema through terminal

php artisan migrate

7. Now we have to make our model, view and controller for that we run this command,

php artisan make:model Project_name -mcr

8. In app folder we find our model

Here we put our database table.

9. Setting Route

10. Set Data in View File in Laravel

In resource/view we make a new folder name parent.blade.php

11. In App/Http/Controller folder we code for function that we call during create, edit, delete operations

11. Now in resource/views we make 4 folder name 1. create.blade.php 2. edit.blade.php 3. view.blade.php 4. index.blade.php

For create.blade.php code

For edit.blade.php code

For view.blade.php code

For index.blade.php code

12. Now we run our local server for this we type

php artisan serve

now we run our project on browser.

Tagged :

CRUD Operation Prerequisites.

Before performing CRUD operations we need to do some of the basic tasks. These includes:- 1. Installing Laravel 2. Creating Laravel Project 3. Making Model 4. Migrating After that we are good to go for performing the operation.

Laravel the most popular PHP-based framework for creating database-driven apps is based on the MVC (Model-View-Controller) architecture and can be used for easily creating apps for making CRUD (Create, Retrieve, Update, Delete) operations in the database.

1. Installing Laravel The first and foremost thing we need to have Laravel and composer installed in our system. the command for installation is
composer global require “laravel/installer=~1.1”
if this does not works then we could just write
composer global require “laravel/installer”
and it would install the file.
2. Creating Laravel Project Then we need to create a project and for this, we need to type
composer create-project –prefer-dist laravel/laravel project_name “5.8.*”
If this does not works then we could just write
composer create-project –prefer-dist laravel/laravel project_name
it would create a Laravel project named blog (with the latest version).

3. Making Model and Migrating After the project is created we need to create the Model. For this we use command.

$ php artisan make:model Model_name -mcr or $ php artisan make:model Model_name -a

(Note:- Model name should begin with a capital letter) Here mcr is (migration controller resources) we can use m/c/r separately but for better convenience, we use a which can equivalently be used in place of “mcr”.

After the model is created we need to create the database and attach it. We can instruct for the database creation through codes or we can go to PHPMyAdmin, and there we can create the database and provide the name of the database in the .env file

After database creation, we need to migrate. Migrations allow to add or drop fields in the database without deleting the records already present. for this we use $ php artisan migrate We could also refresh migration using $ php artisan migrate:refresh

All the prerequisites to perform CRUD operations are completed and are ready to go. Now we can move to views to create the Html page and perform desired CRUD operations.

For CRUD Operation Detailed Explanation with Example. https://www.scmgalaxy.com/tutorials/crud-operation-detailed-explaination-with-example/

Tagged : / / / / / /