Laravel is world famous php framework.it has various features.Laravel is a MVC architecture. In this tutorial will see how to make a crud application in Laravel 8.Laravel 8 – CRUD application tutorial with Example. Here is the Best Place to Learn Laravel 8 Articles.
Lets do the Crud Step by Step Laravel 8 From Scratch
First Step
In computer create the folder Laravel Projects.and open the folder. and type the address bar on cmd
and Press Enter key and Open the Command Prompt.
Install Laravel 8
Create a new Project type the command on the command prompt . I create the project name crudapplication
Laravel Tutorial Setup the Project
1 | composer create-project --prefer-dist laravel/laravel crudapplication |



After Type the Command you have to wait till the project has been created.
Database setup
Create the Database on xampp which name is crud
After created the database.
Change .env File
Change .env File for username, password and DB Name
Remove mb4 from Charset (config/database.php) // MySQL Version < 6
After that run check the application the welcome screen of Laravel framework look like this.
1 | php artisan serve |
Create Migration
Create the tables
1 | php artisan make:migration create_contacts_table |
Click and open it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class CreateContactsTable extends Migration { public function up() { Schema::create('contacts', function (Blueprint $table) { $table->id(); $table->timestamps(); }); } public function down() { Schema::dropIfExists('contacts'); } } |
$table->string(‘name’);
$table->string(‘address’);
$table->string(‘mobile’);
inside the function up() function i shown in below clearly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class CreateContactsTable extends Migration { public function up() { Schema::create('contacts', function (Blueprint $table) { $table->id(); $table->timestamps(); $table->string('name'); $table->string('address'); $table->string('mobile'); }); } public function down() { Schema::dropIfExists('contacts'); } } |
After add the lines type
1 | php artisan migrate |
Create Controller
Create the controller name which is CrudController
1 | php artisan make:controller ContactController --resource |
Create Model
Model is used to get the data from the database.
Create the Model name which is Crud
1 | php artisan make:model Contact |
After Model is Created the look like this. Code inside Model Class (app\Models\)
1 2 3 4 5 6 7 8 9 10 11 | <?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Contact extends Model { use HasFactory; } |
Change it as like this
Add the Namespace above
1 2 3 4 5 6 7 8 9 10 11 | <?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Contact extends Model { protected $table = 'contacts'; protected $primaryKey = 'id'; protected $fillable = ['name', 'address', 'mobile']; } |
Create Views
Create a Folder inside the resources-views
inside the views folder create the contacts folder
In Laravel you have create the pages using pagename.blade.php
Create page layout.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <!DOCTYPE html> <html> <head> <title>Contact Laravel 8 CRUD</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"> </head> <body> <div class="container"> @yield('content') </div> </body> </html> |
Create page index.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | @extends('contacts.layout') @section('content') <div class="container"> <div class="row"> <div class="col-md-9"> <div class="card"> <div class="card-header">Contacts</div> <div class="card-body"> <a href="{{ url('/contact/create') }}" class="btn btn-success btn-sm" title="Add New Contact"> <i class="fa fa-plus" aria-hidden="true"></i> Add New </a> <br/> <br/> <div class="table-responsive"> <table class="table"> <thead> <tr> <th>#</th> <th>Name</th> <th>Address</th> <th>Telephone</th> <th>Actions</th> </tr> </thead> <tbody> @foreach($contacts as $item) <tr> <td>{{ $loop->iteration }}</td> <td>{{ $item->name }}</td> <td>{{ $item->address }}</td> <td>{{ $item->mobile }}</td> <td> <a href="{{ url('/contact/' . $item->id) }}" title="View Student"><button class="btn btn-info btn-sm"><i class="fa fa-eye" aria-hidden="true"></i> View</button></a> <a href="{{ url('/contact/' . $item->id . '/edit') }}" title="Edit Student"><button class="btn btn-primary btn-sm"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> Edit</button></a> <form method="POST" action="{{ url('/contact' . '/' . $item->id) }}" accept-charset="UTF-8" style="display:inline"> {{ method_field('DELETE') }} {{ csrf_field() }} <button type="submit" class="btn btn-danger btn-sm" title="Delete Contact" onclick="return confirm("Confirm delete?")"><i class="fa fa-trash-o" aria-hidden="true"></i> Delete</button> </form> </td> </tr> @endforeach </tbody> </table> </div> </div> </div> </div> </div> </div> @endsection |
Add Records
Create page Create.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | @extends('contacts.layout') @section('content') <div class="card"> <div class="card-header">Contactus Page</div> <div class="card-body"> <form action="{{ url('contact') }}" method="post"> {!! csrf_field() !!} <label>Name</label></br> <input type="text" name="name" id="name" class="form-control"></br> <label>Address</label></br> <input type="text" name="address" id="address" class="form-control"></br> <label>Mobile</label></br> <input type="text" name="mobile" id="mobile" class="form-control"></br> <input type="submit" value="Save" class="btn btn-success"></br> </form> </div> </div> @stop |
Edit Records
Create page edit.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | @extends('contacts.layout') @section('content') <div class="card"> <div class="card-header">Contactus Page</div> <div class="card-body"> <form action="{{ url('contact/' .$contacts->id) }}" method="post"> {!! csrf_field() !!} @method("PATCH") <input type="hidden" name="id" id="id" value="{{$contacts->id}}" id="id" /> <label>Name</label></br> <input type="text" name="name" id="name" value="{{$contacts->name}}" class="form-control"></br> <label>Address</label></br> <input type="text" name="address" id="address" value="{{$contacts->address}}" class="form-control"></br> <label>Mobile</label></br> <input type="text" name="mobile" id="mobile" value="{{$contacts->mobile}}" class="form-control"></br> <input type="submit" value="Update" class="btn btn-success"></br> </form> </div> </div> @stop |
Show Records
Create page show.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | @extends('contacts.layout') @section('content') <div class="card"> <div class="card-header">Contactus Page</div> <div class="card-body"> <div class="card-body"> <h5 class="card-title">Name : {{ $contacts->name }}</h5> <p class="card-text">Address : {{ $contacts->address }}</p> <p class="card-text">Phone : {{ $contacts->mobile }}</p> </div> </hr> </div> </div> |
Controller
After that Pass All view pages through Controller. you have to add the Model namespace here
use App\Models\Contact; Data is coming from the database via Model.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <?php namespace App\Http\Controllers; use App\Models\Contact; use Illuminate\Http\Request; class ContactController extends Controller { public function index() { $contacts = Contact::all(); return view ('contacts.index')->with('contacts', $contacts); } public function create() { return view('contacts.create'); } public function store(Request $request) { $input = $request->all(); Contact::create($input); return redirect('contact')->with('flash_message', 'Contact Addedd!'); } public function show($id) { $contact = Contact::find($id); return view('contacts.show')->with('contacts', $contact); } public function edit($id) { $contact = Contact::find($id); return view('contacts.edit')->with('contacts', $contact); } public function update(Request $request, $id) { $contact = Contact::find($id); $input = $request->all(); $contact->update($input); return redirect('contact')->with('flash_message', 'Contact Updated!'); } public function destroy($id) { Contact::destroy($id); return redirect('contact')->with('flash_message', 'Contact deleted!'); } } |
Routes
Pages are Manage through routes. If you are crud system simple you can add it the routes one line look like this
Route::resource(‘/contact’, ContactController::class);
You have to add the ControllerNameSpace
use App\Http\Controllers\ContactController;
1 2 3 4 5 6 7 8 9 10 11 | <?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\ContactController; Route::get('/', function () { return view('welcome'); }); Route::resource('/contact', ContactController::class); |
i have attached the video link below. which will do this tutorials step by step.