We noticed you're using an ad blocker

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Laravel 7 CRUD tutorial

world cup 2022

FootballDesk

Get update standing EPL, UEFA, LaLiga and more

Get Now

Making a CRUD (Create, Read, Update, Delete) app is very helpful to learn new things. If you are new in Laravel 7 and looking for a step by step tutorial on how to make a Laravel 7 CRUD  app then this post helps you to learn how to make a complete CRUD system using Laravel 7. Before we start we have to make sure our system requirement is okay to use Laravel 7. The Laravel 7 minimum requirements are listed below so that you can confirm either your system is okay or not to install Laravel 7 project.

System Requirements ( According to Laravel 7 official documentation )

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

 

Steps for making Laravel 7 CRUD app

  • Step 01: Create a Laravel 7 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 7 project

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

Replace the laravel7-project 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=laravel7
DB_USERNAME=root
DB_PASSWORD=

Here laravel7 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 7. 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: 2020_03_01_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

 

 

Step 04: Make controller

In our controller, all our business login will be coded to make Laravel 7 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 Laravel 7 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 7 CRUD app is ready to use. To test the Laravel 7 CRUD app operation first, run the server by php artisan serve command and then open your browser and browse http://localhost:8000/contacts

Hope this step by step tutorial on Laravel 7 CRUD app will help you to make your won CRUD system using Laravel 7. If you find this tutorial helpful then please share this with others.


Share on




Related Post - Latest Post


Tinkerpad - A minimal Laravel code editor

Laravel Ajax Datatable CRUD

Laravel 8 CRUD example - Step by step

Laravel Inertia Js CRUD tutorial

Laravel Livewire CRUD tutorial

Laravel 6 jQuery Ajax CRUD tutorial