CRUD application in Laravel

In this tutorial, we are going to create a Laravel application with CRUD operation. In programming, create, read, update, and delete are the four basic operations of data storage. We will give you a simple example of how to perform a CRUD operation in Laravel 8. Laravel is one of the most popular PHP-based frameworks for creating database-driven apps.

Laravel is described by its creators as the framework for web artisans. It’s based on the MVC (Model-View-Controller) pattern and can be used for easily creating apps for making CRUD (Create, Retrieve/Read, Update, Delete) operations on a database.

Create a new Laravel Application

Let’s begin to create our Laravel application with Jetstream authentication. First of all, create a new project.

You can create a fresh Laravel application with one of the below commands:

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

Replace Application_name with your actual project name.

Database Configuration

Let’s configure a database for our application. If you don’t have a database then create a new one and database open the .env file from the root directory of your project.


Replace * with your actual configuration in the above code.

Create Model and Migrations

What is a Model?

In Laravel, the Model is a class that represents the logical structure and relationship of the underlying database table. In Laravel, each of the database tables has a corresponding Model that allows us to interact with that table. Models give you a way to retrieve, insert, and update information into your data table. All of the Laravel Models are stored in the main app directory.

What is Migration?

Migrations are like version control for your database, allowing your team to define and share the application’s database schema definition.

The Laravel Schema facade provides database-agnostic support for creating and manipulating tables across all of Laravel’s supported database systems. Typically, migrations will use this facade to create and modify database tables and columns.

For creating the model and migration enter the below command to the terminal :

php artisan make:model Product -m

This will create a Contact model and a migration file. In the terminal, we get an output similar to:

Model created successfully.
Created Migration: 2019_01_27_193840_create_products_table


namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
    use HasFactory;
    protected $table = 'products';
    public $timestamps = true;

    protected $fillable = [


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductsTable extends Migration
    public function up()
        Schema::create('products', function (Blueprint $table) {

    public function down()

We added the name, description, and price fields in the products table structure.

Migrating to Database

After defining our table structure, we have to create a table in the database using the below command :

php artisan migrate

Creating the Controller

Controllers are meant to group associated request handling logic within a single class. In your Laravel project, they are stored in the app/Http/Controllers directory. The full form of MVC is Model View Controller, which acts as directing traffic among the Views and the Models. After creating the model and migrating our database. Let’s now create the controller for working with the Product model.

In your terminal, run the following command:

php artisan make:controller ProductController

This command will generate a controller at app/Http/Controllers/ProductController.php and update it accordingly :


namespace App\Http\Controllers;

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

class ProductController extends Controller
    public function index()
        $products = Product::latest()->paginate(5);

        return view('index', compact('products'))
            ->with('i', (request()->input('page', 1) - 1) * 5);

    public function store(Request $request)
            'name' => 'required',
            'description' => 'required',
            'price' => 'required'


        return redirect()->back()
            ->with('success', 'Product created successfully.');

    public function edit($id)
        $product = Product::find($id);
        $products = Product::latest()->paginate(5);
        return view('index')->with(['product'=>$product,'products'=>$products]);

    public function update(Request $request)
            'name' => 'required',
            'description' => 'required',
            'price' => 'required'
        $product = Product::find($request->id);

        return redirect()->route('products')->with('success', 'Product updated successfully');

    public function destroy(Request $request)
        $product = Product::find($request->id);
        return redirect()->route('products')->with('success', 'Product deleted successfully');

Creating The Routes

What is Route?

Route is a way of creating a request URL for your application. These URLs do not have to map to specific files on a website. The best thing about these URLs is that they are both human-readable and SEO-friendly.

We have already created a method to handle CRUD for the products table. Now we need to provide implementations for these methods at specific URLs. For that, we have to create routes for particular functions. In Laravel, All web routes must be added to a particular file routes/web.php. although you can create a custom route file and bind it to your application.

In this example, we are going to use the default route file so Open the routes/web.php file and update it accordingly


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;

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

Route::get('/products', [ProductController::class, 'index'])->name('products');
Route::post('/products', [ProductController::class, 'store'])->name('');
Route::get('/products/edit/{id}', [ProductController::class, 'edit'])->name('products.edit');
Route::post('/products/update', [ProductController::class, 'update'])->name('products.update');
Route::post('/products/delete', [ProductController::class, 'destroy'])->name('products.delete');

Creating Views

View performs all frontend functionality of our application like displaying data, getting user input or getting requests. In this example, we are going to perform CRUD into a single view you can use as many views as you want.

For Creating views, you can create a file with the .blade.php extension at resources/views/ directory. For this example, Create an index.blade.php file in that directory and alter it as below :

        <title>Simple Laravel CRUD Example</title>
        <link href="" rel="stylesheet">
        <script defer src="" integrity="sha384-tzzSw1/Vo+0N5UhStP3bvwWPq+uvzCMfrN1fEFe+xBmv1C/AtVX5K0uZtmcHitFZ" crossorigin="anonymous"></script>
        <script defer src=""integrity="sha384-6OIrr52G08NpOFSZdxxz1xdNSndlD4vdcf/q2myIUVO0VsqaGHJsB0RaBE01VTOY" crossorigin="anonymous"></script>
        <div class="container">
            <div class="row" style="margin-top: 10px;">
                <div class="col-lg-12 margin-tb">
                    <div class="text-center">
                        <h1>Simple Laravel CRUD Example </h1>
            @if ($message = Session::get('success'))
                <div class="alert alert-success">
            @if ($errors->any())
                <div class="alert alert-danger">
                        @foreach ($errors->all() as $error)
            <div class="row">
                    <form action="{{route('products.update')}}" method="POST" >
                    <input type="hidden" name="id" value="{{$product->id}}" >
                    <form action="{{route('')}}" method="POST" >
                    <div class="row mt-4">
                        <div class="col-xs-6 col-sm-6 col-md-6">
                            <div class="form-group">
                                <input type="text" name="name" class="form-control" placeholder="Name" value="{{isset($product) ? $product->name : ''}}">
                        <div class="col-xs-6 col-sm-6 col-md-6">
                            <div class="form-group">
                                <input type="number" name="price" class="form-control" placeholder="Put the price" value="{{isset($product) ? $product->price : ''}}">
                        <div class="col-xs-12 col-sm-12 col-md-12">
                            <div class="form-group">
                                <textarea class="form-control" rows="3" name="description"
                                    placeholder="description">{{isset($product) ? $product->name : ''}}</textarea>
                        <div class="col-xs-12 col-sm-12 col-md-12 text-right">
                            <button type="submit" class="btn btn-primary">Submit</button>
                            <a href="{{route('products')}}" class="btn btn-danger">Cancel</a>
            <table class="table table-bordered table-responsive-lg">
                @foreach ($products as $product)
                            <form action="{{route('products.delete')}}" method="POST">
                                <a href="{{route('products.edit',$product->id)}}">
                                    <i class="fas fa-edit  fa-lg"></i>

                                <input type="hidden" name="id" value="{{$product->id}}">
                                <button type="submit" title="delete" style="border: none; background-color:transparent;">
                                    <i class="fas fa-trash fa-lg text-danger"></i>
            {!! $products->links() !!}

Running Laravel CRUD Application

For Running or Testing the CRUD application enter the below command :

php artisan serve

It will produce output like the below :

Starting Laravel development server:
PHP 7.4.6 Development Server ( started

Open your browser go to the below URL and test your application.