Crud Operation in Laravel for beginners with scratch

Amit Kumar
3 min readApr 20, 2022

--

In this tutorial I'm going to learn about crud operation in Laravel with step by step.

composer create-project laravel/laravel crud "5.8.*"

next to connect with database go to .env file and put your database name

Next to create model migration and controller file so run below command

php artisan make:controller ProductController
php artisan make:model -m

Next go to your product migration file and these column define below

$table->bigIncrements('id');
$table->string('name');
$table->string('email');
$table->string('address');

Now migrate the table

php artisan migrate

Next go to your controller and add crud function

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Product;


class ProductController extends Controller
{
public function index(){

$products = Product::latest()->get();
return view('products.index', compact('products'));
}

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

public function store(Request $request){
$products = new Product;
$products->name = $request->name;
$products->email = $request->email;
$request->address = $request->address;
$products->save();
return redirect('/products');
}

public function edit($id)
{
$products = Product::find($id);
return view('products.edit',compact('products'));
}

public function update(Request $request)
{
$products = Product::find($request->id);
$products->name = $request->name;
$products->email = $request->email;
$products->address = $request->address;
$products->update();
return redirect('/products');
}

public function delete(Request $request )
{
$products = Product::find($request->id);
$products->delete();
return redirect('/products');

}
}

Next go to route and add below route urls

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
return view('welcome');
});

Route::get('/products', 'ProductController@index')->name('product.index');
Route::get('/products/create','ProductController@create')->name('product.create');
Route::post('/products/store','ProductController@store')->name('product.store');
Route::get('/products/edit/{id}','ProductController@edit')->name('product.edit');
Route::post('/products/update','ProductController@update')->name('product.update');
Route::get('/products/delete/{id}','ProductController@delete')->name('product.delete');

Next go to resource/view/products create below filename

  • create.blade.php
  • edit.blade.php
  • index.blade.php

Go to create blade php and add below code in their files

@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<form action="{{ route('product.store')}}" method="POST">
@csrf
<div class="form-group">
<label for="exampleInputEmail1">Name</label>
<input type="text" class="form-control" id="name" name="name" aria-describedby="emailHelp" placeholder="Enter name">

</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" name="email" placeholder="email">
</div>
<div class="form-group">

<input class="form-control" type="text" name="address" id="address" placeholder="enter your address">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
@endsection

index.blade.php

@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Address</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
@foreach ($products as $amit )


<tr>
<th scope="row">1</th>
<td>{{$amit->name}}</td>
<td>{{$amit->email}}</td>
<td>{{$amit->address}}</td>
<td>
<a href="{{ route('product.edit', $amit->id)}}">Edit</a>
<a href="{{ route('product.delete', $amit->id)}}">Delete</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
@endsection

edit.blade.php

@extends('layouts.app')
@section('content')

<div class="container">
<div class="row">
<form action="{{ route('product.update')}}" method="POST">
@csrf
<input type="hidden" name="id" value="{{$products->id}}">
<div class="form-group">
<label for="exampleInputEmail1">Name</label>
<input type="text" class="form-control" id="name" name="name" value="{{$products->name}}" aria-describedby="emailHelp" placeholder="Enter name">
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" value="{{$products->email}}" name="email" placeholder="email">
</div>
<div class="form-group">

<input class="form-control" type="text" value="{{$products->address}}" name="address" id="address" placeholder="enter your address">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
@endsection

Now its working properly

--

--

Amit Kumar

Hi I am Amit Experienced Web Developer with a demonstrated history of working in the information technology and services industry.