Laravel 6 CRUD tutorial step by step


Are you new to Laravel Framework? and looking for a complete tutorial on how to make CRUD (Create, Read, Update, Delete) app in the Laravel framework. This is the right post to learn how to make a complete CRUD system using the Laravel framework. This is a step by step tutorial for beginner learner. So don't worry if you are new to the Laravel framework.

Before we start we have to make sure our system requirement is okay to use Laravel 6. The Laravel 6 minimum requirements are listed below so that you can confirm either your system is okay or not to install Laravel 6 project.

System Requirements ( According to Laravel 6 official documentation )

  • PHP >= 7.2.0
  • BCMath PHP Extension
  • Ctype PHP Extension
  • JSON PHP Extension
  • Mbstring PHP Extension
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Tokenizer PHP Extension
  • XML PHP Extension

Follow the steps one by one to make Laravel 6 CRUD

Laravel 6 CRUD steps overview

  • Step 01: Create a Laravel project
  • Step 02: Database Configuration
  • Step 03: Make model & migration
  • Step 04: Make controller
  • Step 05: Define routes
  • Step 06: Make views

Step 01: Create a Laravel project

composer create-project --prefer-dist laravel/laravel project-name

Replace the project-name with your project name. According to this name, a folder will create in your project directory.

Step 02: Database Configuration

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel6
DB_USERNAME=root
DB_PASSWORD=

Here laravel6 is our database name. If your database name different then update it and save. Our project creation finished and the database is ready to use.

Step 03: Make model & migration

We will make a contact list CRUD using Laravel 6. So that we need a contacts table in our database. Here we do not create the table manually. Here we use Laravel migration. When we'll run our migration that will make the table for us. Run the command in your terminal.

php artisan make:model Contact -m

When you run the command, you see the output like this.

Model created successfully.
Created Migration: 2019_11_05_151010_create_contacts_table

Now open the migration file from database/migrations folder of your project and replace the code with below.

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateContactsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('contacts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email');
            $table->string('phone');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('contacts');
    }
} 

Our migration file is ready. Now run the migration with this command. This command will create our tables in our database.

php artisan migrate

If this command successfully runs then you will see this kind of output on your screen.

 

laravel-migration.png

Step 04: Make controller

In our controller, all our business login will be coded to make CRUD system. To make the controller run the command.

php artisan make:controller ContactController

By this command, a file will be created in app/Http/Controllers name with ContactController.php. Write the code below in the ContactController.php

<?php namespace App\Http\Controllers;

use App\Contact;
use Response;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class ContactController extends Controller
{

    public function index()
    {
        $data = Contact::orderBy('id','desc')->paginate(10)->setPath('contacts');
        return view('admin.contacts.index',compact(['data']));
    }

    public function create()
    {
        return view('admin.contacts.create');
    }

    public function store(Request $request)
    {
        $request->validate([
         'name' => 'required',
         'email' => 'required|email',
         'phone' => 'required'
        ]);

        Contact::create($request->all());
        return redirect()->back()->with('success','Create Successfully');
    }

    public function show($id)
    {
       $data =  Contact::find($id);
       return view('admin.contacts.show',compact(['data']));
    }

    public function edit($id)
    {
       $data = Contact::find($id);
       return view('admin.contacts.edit',compact(['data']));
    }

    public function update(Request $request, $id)
    {
        $request->validate([
         'name' => 'required',
         'email' => 'required|email',
         'phone' => 'required'
        ]);

        Contact::where('id',$id)->update($request->all());
        return redirect()->back()->with('success','Update Successfully');
        
    }

    public function destroy($id)
    {
        Contact::where('id',$id)->delete();
        return redirect()->back()->with('success','Delete Successfully');
    }

}

Step 05: Define routes

Open web.php file from routes folder and write the routes like below.

Route::resource('contacts','ContactController');

Here we are using the Laravel resource route which will make all our required routes that are needed for CURD operation.

Step 06: Make views

Here is the final part, We need some forms and HTML markup to show our records and data insert, update. Let's make those views. Create a folder inside views folder name with contacts so that all views are related to contact CRUD will be in the same folder and organized.

We need the Laravel Collective package. Install it by the composer.

composer require laravelcollective/html

Create an index.blade.php to show all our records from the database.

@extends('layout') @section('content')
<div class="col-md-12">

    <div class="table-responsive">
        <table class="table table-bordered table-condensed table-striped">
            <thead>

                <th>ID</th>
                <th>NAME</th>
                <th>EMAIL</th>
                <th>PHONE</th>

                <th>ACTION</th>
            </thead>

            <tbody>
                @foreach($data as $row)
                <tr>

                    <td>{{$row->id }}</td>
                    <td>{{$row->name }}</td>
                    <td>{{$row->email }}</td>
                    <td>{{$row->phone }}</td>

                    <td>
                        <a href="{{ route('contacts.edit', $row->id)}}" class="btn btn-primary">Edit</a>

                        <form action="{{ route('contacts.destroy', $row->id)}}" method="post">
                            @csrf @method('DELETE')
                            <button class="btn btn-danger" type="submit">Delete</button>
                        </form>

                    </td>
                </tr>
                @endforeach
            </tbody>

        </table>
    </div>
    <div>
        <?php echo $data->render(); ?>
    </div>
</div>

@endsection

Create a create.blade.php file for insert data.

@extends('layout')

@section('content')
{!! Form::open(['action' =>'ContactController@store', 'method' => 'POST','files'=>true])!!}
    
    <div class="col-md-6">
        
        
			 <div class="form-group required">
				{!! Form::label("NAME") !!}
				{!! Form::text("name", null ,["class"=>"form-control","required"=>"required"]) !!}
			</div>

			 <div class="form-group required">
				{!! Form::label("EMAIL") !!}
				{!! Form::text("email", null ,["class"=>"form-control","required"=>"required"]) !!}
			</div>

			 <div class="form-group required">
				{!! Form::label("PHONE") !!}
				{!! Form::text("phone", null ,["class"=>"form-control","required"=>"required"]) !!}
			</div>


   
        <div class="well well-sm clearfix">
            <button class="btn btn-success pull-right" title="Save" type="submit">Create</button>
        </div>
    </div>
 
{!! Form::close() !!}
@endsection

Create an edit.blade.php file to edit data.

@extends('layout')

@section('content')
    {!! Form::open(['action' =>['ContactController@update',$data->id], 'method' => 'PUT','files'=>true])!!}
    
        <div class="col-md-6">

            
			 <div class="form-group required">
				{!! Form::label("NAME") !!}
				{!! Form::text("name", $data->name ,["class"=>"form-control","required"=>"required"]) !!}
			</div>

			 <div class="form-group required">
				{!! Form::label("EMAIL") !!}
				{!! Form::text("email", $data->email ,["class"=>"form-control","required"=>"required"]) !!}
			</div>

			 <div class="form-group required">
				{!! Form::label("PHONE") !!}
				{!! Form::text("phone", $data->phone ,["class"=>"form-control","required"=>"required"]) !!}
			</div>



            <div class="well well-sm clearfix">
                <button class="btn btn-success pull-right" title="Save" type="submit">Update</button>
            </div>
        </div>
        
    {!! Form::close() !!}
@endsection

Now our Laravel 6 CRUD is ready with pagination and also proper validation. To test the CRUD operation first run the server by php artisan serve command and then open your browser and browse http://localhost:8000/contacts


Share on



Related Post - Latest Post


Laravel Barcode generation tutorial

Laravel 8 CRUD example - Step by step

Laravel Inertia Js CRUD tutorial

Laravel 7 CRUD tutorial

Laravel Livewire CRUD tutorial

Laravel 6 jQuery Ajax CRUD tutorial