You know that, Laravel releases its new version every year in the month of february. This year they also release their latest version and which is Laravel 10. So I am here to show you a complete overview of Laravel 10 with the complete tutorial for creating an employee crud system. In this tutorial, we will create a crud system in Laravel 10.

I will create an Employee model and table which have some attributes like name, email, joining_date, joining_salary, and is_active, and using these fields, we will make a crud application in Laravel 10. I will use an eloquent model mapper like ORM to create this crud application in Laravel 10. As I am going to use a resource controller so you will also learn how to update data in laravel using resource controller?

In this Laravel 10 tutorial, you will learn how to create a resource controller, how to create a route with a resource controller, and how to create a crud application using this resource controller. Now let's see some preview images of this tutorial of Laravel 10 complete crud application for beginners. 

Employee List Page

laravel-10-tutorial

Employee Create Page:

laravel-10-tutorial-create-new-employee

Employee Edit Page:

laravel-10-crud-tutorial-update-employee-form

Step 1: Download Fresh Laravel

In the first step of how to perform CRUD operation in Laravel, download a fresh Laravel application by the following command:

composer create-project laravel/laravel example-app

 

Step 2: Create Migration

In this step, we need to create a migration and model for generating a employees table. So run the below command to create employees table.

php artisan make:model Employee -m

 

Now update the migration like below:

database/migrations

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('employees', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->date('joining_date')->nullable();
            $table->integer('joining_salary');
            $table->boolean('is_active')->default(false);
            $table->timestamps();
        });
    }

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

 

And now update the model like along with protected property guard to avoid mass assignment error:

app/Models/Employee.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    use HasFactory;

    protected $guarded = [];
}

 

Step 3: Connect database

In this step, we will connect the database, and I am going to use MySQL. So connect it like the below by updating the .env file:

.env

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

 

Now run the migration command:

php artisan migrate

 

Step 4:  Create Route

Now in this, create a resource route like the below to complete the crud application in Laravel 10. I am going to use a resource controller. So define the resource route like the below:

routes/web.php

<?php

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

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::resource('employee', EmployeeController::class);

 

Read also:  Laravel 10 CRUD Tutorial Using Query Builder

 

Step 5: Create Controller

Now in this step, we have to create a country controller to define this method to create an eloquent orm crud application in laravel 10.

php artisan make:controller EmployeeController -r

 

Now update the controller like below. Here index() method is for showing the employee list, create() method is for showing the store employee form, the update() method is for updating the employee form, and the delete() method is for deleting employees from the database.

app/Http/Controllers/EmployeeCountroller.php

<?php

namespace App\Http\Controllers;

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

class EmployeeController extends Controller
{
    public function index()
    {
        $employees = Employee::orderBy('id', 'desc')->paginate(5);

        return view('employee.index', compact('employees'));
    }

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

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required|email|unique:employees,email',
            'joining_date' => 'required',
            'joining_salary' => 'required'
        ]);

        Employee::create($request->except('_token'));

        return redirect()->route('employee.index')
            ->withSuccess('Employee has been created successfully.');
    }

    public function show(Employee $employee)
    {
        return view('employee.show', compact('employee'));
    }

    public function edit(Employee $employee)
    {
        return view('employee.edit', compact('employee'));
    }

    public function update(Request $request, Employee $employee)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required|email|unique:employees,email,' . $employee->id,
            'joining_date' => 'required',
            'joining_salary' => 'required'
        ]);

        $employee->update($request->all());

        return redirect()->route('employee.index')
            ->withSuccess('Employee details has been updated successfully.');
    }

    public function destroy(Employee $employee)
    {
        $employee->delete();

        return redirect()->route('employee.index')
            ->withSuccess('Employee has been delete successfully.');
    }
}

 

Step 6: Create Views

We are almost there. Just we have to create four views files

  • index.balde.php
  • create.blade.php
  • edit.blade.php
  • show.blade.php
  • app.blade.php

So create these files inside the following path and update them like below:

resources/views/layouts/app.blade.php

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>{{ config('app.name', 'Laravel') }}</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body style="background: #ddd;">
    <div id="app">
        <nav class="navbar navbar-default navbar-static-top">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-navbar-collapse" aria-expanded="false">
                        <span class="sr-only">Toggle Navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="{{ url('/') }}">Laravelia</a>
                </div>
                <div class="collapse navbar-collapse" id="app-navbar-collapse">
                    <ul class="nav navbar-nav">
                        &nbsp;
                    </ul>
                        <ul class="nav navbar-nav navbar-right">
                        @guest
                            <li><a href="{{ route('login') }}">Login</a></li>
                            <li><a href="{{ route('register') }}">Register</a></li>
                        @else
                            <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true">
                                    {{ Auth::user()->name }} <span class="caret"></span>
                                </a>
        
                                <ul class="dropdown-menu">
                                    <li>
                                        <a href="{{ route('account.index') }}">Account</a>
                                    </li>
                                    <li>
                                        <a href="{{ route('logout') }}"
                                            onclick="event.preventDefault();
                                                     document.getElementById('logout-form').submit();">
                                            Logout
                                        </a>
        
                                        <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                                            {{ csrf_field() }}
                                        </form>
                                    </li>
                                </ul>
                            </li>
                        @endguest
                    </ul>
                </div>
            </div>
        </nav>        
        <div class="container">
            <div class="col-md-2"></div>
            <div class="col-md-8">
                @yield('content')
            </div>
            <div class="col-md-2"></div>
        </div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</body>
</html>

 

This page for showing all the employee lists.

resources/views/employee/index.blade.php

@extends('layouts.app')

@section('content')
    @if (session()->has('success'))
        <div class="alert alert-success">
            {{ session()->get('success') }}
        </div>
    @endif
    <div class="panel panel-default">
        <div class="panel-body">
            <strong>Employee List</strong>
            <a href="{{ route('employee.create') }}" class="btn btn-primary btn-xs pull-right py-0">Create Employee</a>
            <table class="table table-responsive table-bordered table-stripped" style="margin-top:10px;">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Joining Date</th>
                        <th>Status</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($employees as $employee)
                    <tr>
                        <td>{{ $employee->id }}</td>
                        <td>{{ $employee->name }}</td>
                        <td>{{ $employee->email }}</td>
                        <td>{{ $employee->joining_date }}</td>
                        <td><span type="button" class="btn btn-{{ $employee->is_active ? 'success' : 'danger' }} btn-xs py-0">{{ $employee->is_active ? 'Active' : 'Inactive' }}</span></td>
                        <td>
                            <a href="{{ route('employee.show',$employee->id) }}" class="btn btn-primary btn-xs py-0">Show</a>
                            <a href="{{ route('employee.edit',$employee->id) }}" class="btn btn-warning btn-xs py-0">Edit</a>
                            <form action="{{ route('employee.destroy',$employee->id) }}" method="post">
                                @csrf
                                @method('delete')
                                <button type="submit" class="btn btn-danger btn-xs py-0">Delete</button>
                            </form>
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
            {{ $employees->links() }}
        </div>
    </div>
@endsection

 

This page is for creating an employee.

resources/views/employee/create.blade.php

@extends('layouts.app')

@section('content')
    <div class="panel panel-default">
        <div class="panel-body">
            <p style="font-size:20px; font-weight:bold;">Create New Employee</p>
            <form action="{{ route('employee.store') }}" method="POST">
                @csrf
                <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                    <label for="name">Name</label>
                    <input type="text" name="name" id="name" class="form-control">

                    @if ($errors->has('name'))
                        <span class="help-block">
                            <strong>{{ $errors->first('name') }}</strong>
                        </span>
                    @endif
                </div>

                <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                    <label for="email">Email</label>
                    <input type="email" name="email" id="email" class="form-control">

                    @if ($errors->has('email'))
                        <span class="help-block">
                            <strong>{{ $errors->first('email') }}</strong>
                        </span>
                    @endif
                </div>

                <div class="form-group{{ $errors->has('joining_date') ? ' has-error' : '' }}">
                    <label for="joining_date">Joining date</label>
                    <input type="date" name="joining_date" id="joining_date" class="form-control">

                    @if ($errors->has('joining_date'))
                        <span class="help-block">
                            <strong>{{ $errors->first('joining_date') }}</strong>
                        </span>
                    @endif
                </div>

                <div class="form-group{{ $errors->has('joining_salary') ? ' has-error' : '' }}">
                    <label for="joining_salary">Joining salary</label>
                    <input type="number" name="joining_salary" id="joining_salary" class="form-control">

                    @if ($errors->has('joining_salary'))
                        <span class="help-block">
                            <strong>{{ $errors->first('joining_salary') }}</strong>
                        </span>
                    @endif
                </div>

                <div class="form-group{{ $errors->has('is_active') ? ' has-error' : '' }}">
                    <label for="is_active">Active</label><br>
                    <input type="checkbox" name="is_active" id="is_active" value="1">
                    @if ($errors->has('is_active'))
                        <span class="help-block">
                            <strong>{{ $errors->first('is_active') }}</strong>
                        </span>
                    @endif
                </div>

                <button type="submit" class="btn btn-primary">Create Employee</button>
            </form>
        </div>
    </div>
@endsection

 

This page is for editing an employee.

resources/views/employee/edit.blade.php

@extends('layouts.app')

@section('content')
    <div class="panel panel-default">
        <div class="panel-body">
            <p style="font-size:20px; font-weight:bold;">Update Employee</p>
            <form action="{{ route('employee.update',$employee->id) }}" method="POST">
                @csrf
                @method('put')
                <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
                    <label for="name">Name</label>
                    <input type="text" name="name" id="name" class="form-control" value="{{ $employee->name }}">

                    @if ($errors->has('name'))
                        <span class="help-block">
                            <strong>{{ $errors->first('name') }}</strong>
                        </span>
                    @endif
                </div>

                <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
                    <label for="email">Email</label>
                    <input type="email" name="email" id="email" class="form-control" value="{{ $employee->email }}">

                    @if ($errors->has('email'))
                        <span class="help-block">
                            <strong>{{ $errors->first('email') }}</strong>
                        </span>
                    @endif
                </div>

                <div class="form-group{{ $errors->has('joining_date') ? ' has-error' : '' }}">
                    <label for="joining_date">Joining date</label>
                    <input type="date" name="joining_date" id="joining_date" class="form-control" value="{{ $employee->joining_date }}">

                    @if ($errors->has('joining_date'))
                        <span class="help-block">
                            <strong>{{ $errors->first('joining_date') }}</strong>
                        </span>
                    @endif
                </div>

                <div class="form-group{{ $errors->has('joining_salary') ? ' has-error' : '' }}">
                    <label for="joining_salary">Joining salary</label>
                    <input type="number" name="joining_salary" id="joining_salary" class="form-control" value="{{ $employee->joining_salary }}">

                    @if ($errors->has('joining_salary'))
                        <span class="help-block">
                            <strong>{{ $errors->first('joining_salary') }}</strong>
                        </span>
                    @endif
                </div>

                <div class="form-group{{ $errors->has('is_active') ? ' has-error' : '' }}">
                    <label for="is_active">Active</label><br>
                    <input type="checkbox" name="is_active" id="is_active" value="1" {{ $employee->is_active ? 'checked' : '' }}>
                    @if ($errors->has('is_active'))
                        <span class="help-block">
                            <strong>{{ $errors->first('is_active') }}</strong>
                        </span>
                    @endif
                </div>

                <button type="submit" class="btn btn-primary">Update Employee</button>
            </form>
        </div>
    </div>
@endsection

 

This page is for showing an employee.

resources/views/employee/show.blade.php

@extends('layouts.app')

@section('content')
    <div class="panel panel-default">
        <div class="panel-body">
            <p style="font-size:20px; font-weight:bold;">Employee details</p>
            <div class="form-group">
                <label for="name">Name</label>
                <input type="text" class="form-control" value="{{ $employee->name }}">
            </div>

            <div class="form-group">
                <label for="email">Email</label>
                <input type="email" class="form-control" value="{{ $employee->email }}">
            </div>

            <div class="form-group">
                <label for="joining_date">Joining date</label>
                <input type="date" class="form-control" value="{{ $employee->joining_date }}">
            </div>

            <div class="form-group">
                <label for="joining_salary">Joining salary</label>
                <input type="number" class="form-control" value="{{ $employee->joining_salary }}">
            </div>

            <div class="form-group">
                <label for="is_active">Active</label><br>
                <input type="checkbox"  {{ $employee->is_active ? 'checked' : '' }}/>
            </div>
            <a href="{{ route('employee.index') }}" class="btn btn-primary">Back</button>
        </div>
    </div>
@endsection

 

Now all are set to go. Now run php artisan serve command to start the development server and visit the following URL to check this Laravel 10 crud application:

 

URL
http://localhost:8000/employee

 

Conclusion

I have tried my best to show you this laravel 10 crud application tutorial. Now we know what is the latest version of Laravel 2023. Hope this laravel 10 crud operation step by step tutorial will help you to create how to perform CRUD operation in Laravel.

Category : #laravel

Tags : #laravel , #laravel crud