
Laravel 11 CRUD Example Tutorial
In this tutorial, We will explain to you how to create laravel 11 crud example tutorial. we give information of the laravel 11 crud example from scratch for beginners.
Laravel is the most popular framework of PHP. laravel is better than other PHP frameworks because it handles the command base. so let us see about laravel 11 crud Operation example tutorial. it was released on March 12, 2024.
Now, we follow the below steps for creating the laravel 11 CRUD operation(Laravel 11 CRUD example). so you can see our laravel 11 tutorials.
Overview
Step 1: Install Laravel 11
Step 2: MySQL Database Configuration
Step 3: Create Migration
Step 4: Create Form Request Validation Class
Step 5: Create Controller and Model
Step 6: Add Resource Route
Step 7: Update AppServiceProvider
Step 8: Add Blade Files
Step 9: Run Laravel Application
Step 1: Install Laravel 11
We are going to create the Laravel 11 App and install laravel 11, so first open the command prompt or terminal and go to xampp htdocs folder directory using the command prompt. after then run the below command for laravel 11 install.
1 | composer create-project laravel/laravel:^11.0 laravel_11_crud |
Step 2: MySQL Database Configuration
After the complete installation of laravel. we have to do database configuration. now we will open the .env file and change the database name, username, and password in the .env file. See below for changes in a .env file.
1 2 3 4 5 6 7 8 | DB_CONNECTION=mysql DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_11_crud DB_USERNAME=root DB_PASSWORD= DB_COLLATION=utf8mb4_unicode_ci |
Step 3: Create Migration
Now, We need to create a migration. so we will below the command using create the students table migration.
1 | php artisan make:migration create_students_table --create=students |
After complete migration. we need below changes in the database/migrations/create_students_table file.
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 | <?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('students', function (Blueprint $table) { $table->id(); $table->string('first_name'); $table->string('last_name'); $table->text('address'); $table->timestamps(); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('students'); } }; ?> |
Run the below command. after the changes above file.
1 | php artisan migrate |
Step 4: Create Form Request Validation Class
in this step, we will create form validation class for create and update student form.
so first here create class for store(), below command using you can easily create class.
1 | php artisan make:request StudentStoreRequest |
paste below code into your file.
app/Http/Requests/StudentStoreRequest.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 | <?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class StudentStoreRequest extends FormRequest { /** * Determine if the user is authorized to make this request. */ public function authorize(): bool { return true; } /** * Get the validation rules that apply to the request. * * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> */ public function rules(): array { return [ 'first_name' => 'required', 'last_name' => 'required', 'address' => 'required' ]; } } ?> |
second class create for update(),below command using create it.
1 | php artisan make:request StudentUpdateRequest |
paste below code into your file.
app/Http/Requests/StudentUpdateRequest.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 | <?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; class StudentUpdateRequest extends FormRequest { /** * Determine if the user is authorized to make this request. */ public function authorize(): bool { return true; } /** * Get the validation rules that apply to the request. * * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> */ public function rules(): array { return [ 'first_name' => 'required', 'last_name' => 'required', 'address' => 'required' ]; } } ?> |
Step 5: Create Controller and Model
Here below command help to create the controller and model.
1 | php artisan make:controller StudentController --resource --model=Student |
in this file, we will create student model, and pass the ours fields in this model.
app/Models/Student.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Student extends Model { use HasFactory; protected $fillable = [ 'first_name', 'last_name', 'address', ]; } ?> |
in this file, we will create student controller, we will create some method for crud operation. so you can follow below file.
app/Http/Controllers/StudentController.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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | <?php namespace App\Http\Controllers; use App\Models\Student; use Illuminate\View\View; use Illuminate\Http\Request; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Response; use App\Http\Requests\StudentStoreRequest; use App\Http\Requests\StudentUpdateRequest; class StudentController extends Controller { /** * Display a listing of the resource. */ public function index(): View { $students = Student::latest()->paginate(5); return view('students.index', compact('students'))->with('i', (request()->input('page', 1) - 1) * 5); } /** * Show the form for creating a new resource. */ public function create(): View { return view('students.create'); } /** * Store a newly created resource in storage. */ public function store(StudentStoreRequest $request): RedirectResponse { Student::create($request->validated()); return redirect()->route('students.index')->with('success', 'Student created successfully.'); } /** * Display the specified resource. */ public function show(Student $student): View { return view('students.show',compact('student')); } /** * Show the form for editing the specified resource. */ public function edit(Student $student): View { return view('students.edit',compact('student')); } /** * Update the specified resource in storage. */ public function update(StudentUpdateRequest $request, Student $student): RedirectResponse { $student->update($request->validated()); return redirect()->route('students.index')->with('success','Student updated successfully'); } /** * Remove the specified resource from storage. */ public function destroy(Student $student): RedirectResponse { $student->delete(); return redirect()->route('students.index')->with('success','Student deleted successfully'); } } ?> |
Step 6: Add Resource Route
We have to need put below student resource route in routes/web.php. so you can below routes file.
1 2 3 4 5 6 7 8 9 10 11 12 | <?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\StudentController; Route::get('/', function () { return view('welcome'); }); Route::resource('students', StudentController::class); ?> |
Step 7: Update AppServiceProvider
In this file, we will use the boostrap 5 and paginator. so you can see below file.
app/Provides/AppServiceProvider.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 | <?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Pagination\Paginator; class AppServiceProvider extends ServiceProvider { /** * Register any application services. */ public function register(): void { // } /** * Bootstrap any application services. */ public function boot(): void { Paginator::useBootstrapFive(); } } ?> |
Step 8: Add Blade Files
So finally, first we will create the new directory “resources/views/students/”. in this directory, we will create layout.blade.php, index.blade.php, create.blade.php, edit.blade.php and show.blade.php files.
resources/views/students/layout.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <!DOCTYPE html> <html> <head> <title>Laravel 11 CRUD Example Tutorial - xpertphp.com</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" /> </head> <body> <div class="container"> @yield('content') </div> </body> </html> |
resources/views/students/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 56 57 58 59 60 61 62 | @extends('students.layout') @section('content') <div class="card mt-5"> <h2 class="card-header">Laravel 11 CRUD Example Tutorial - xpertphp.com</h2> <div class="card-body"> @session('success') <div class="alert alert-success" role="alert"> {{ $value }} </div> @endsession <div class="d-grid gap-2 d-md-flex justify-content-md-end"> <a class="btn btn-success btn-sm" href="{{ route('students.create') }}"> <i class="fa fa-plus"></i> Create New Student</a> </div> <table class="table table-bordered table-striped mt-4"> <thead> <tr> <th width="80px">No</th> <th>First Name</th> <th>Last Name</th> <th>Address</th> <th width="250px">Action</th> </tr> </thead> <tbody> @forelse ($students as $student) <tr> <td>{{ ++$i }}</td> <td>{{ $student->first_name }}</td> <td>{{ $student->last_name }}</td> <td>{{ $student->address }}</td> <td> <form action="{{ route('students.destroy',$student->id) }}" method="POST"> <a class="btn btn-info btn-sm" href="{{ route('students.show',$student->id) }}"><i class="fa-solid fa-list"></i> Show</a> <a class="btn btn-primary btn-sm" href="{{ route('students.edit',$student->id) }}"><i class="fa-solid fa-pen-to-square"></i> Edit</a> @csrf @method('DELETE') <button type="submit" class="btn btn-danger btn-sm"><i class="fa-solid fa-trash"></i> Delete</button> </form> </td> </tr> @empty <tr> <td colspan="5">No data Found.</td> </tr> @endforelse </tbody> </table> {!! $students->links() !!} </div> </div> @endsection |
resources/views/students/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 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 | @extends('students.layout') @section('content') <div class="card mt-5"> <h2 class="card-header">Add New Student</h2> <div class="card-body"> <div class="d-grid gap-2 d-md-flex justify-content-md-end"> <a class="btn btn-primary btn-sm" href="{{ route('students.index') }}"><i class="fa fa-arrow-left"></i> Back</a> </div> <form action="{{ route('students.store') }}" method="POST"> @csrf <div class="mb-3"> <label for="inputName" class="form-label"><strong>First Name:</strong></label> <input type="text" name="first_name" class="form-control @error('first_name') is-invalid @enderror" id="inputName" placeholder="First Name"> @error('first_name') <div class="form-text text-danger">{{ $message }}</div> @enderror </div> <div class="mb-3"> <label for="inputName" class="form-label"><strong>Last Name:</strong></label> <input type="text" name="last_name" class="form-control @error('last_name') is-invalid @enderror" id="inputName" placeholder="Last Name"> @error('last_name') <div class="form-text text-danger">{{ $message }}</div> @enderror </div> <div class="mb-3"> <label for="inputDetail" class="form-label"><strong>Address:</strong></label> <textarea class="form-control @error('address') is-invalid @enderror" style="height:150px" name="address" id="inputDetail" placeholder="Address"></textarea> @error('detail') <div class="form-text text-danger">{{ $message }}</div> @enderror </div> <button type="submit" class="btn btn-success"><i class="fa-solid fa-floppy-disk"></i> Submit</button> </form> </div> </div> @endsection |
resources/views/students/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 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 60 61 62 | @extends('students.layout') @section('content') <div class="card mt-5"> <h2 class="card-header">Edit Student</h2> <div class="card-body"> <div class="d-grid gap-2 d-md-flex justify-content-md-end"> <a class="btn btn-primary btn-sm" href="{{ route('students.index') }}"><i class="fa fa-arrow-left"></i> Back</a> </div> <form action="{{ route('students.update',$student->id) }}" method="POST"> @csrf @method('PUT') <div class="mb-3"> <label for="inputName" class="form-label"><strong>First Name:</strong></label> <input type="text" name="first_name" value="{{ $student->first_name }}" class="form-control @error('first_name') is-invalid @enderror" id="inputName" placeholder="First Name"> @error('first_name') <div class="form-text text-danger">{{ $message }}</div> @enderror </div> <div class="mb-3"> <label for="inputName" class="form-label"><strong>Last Name:</strong></label> <input type="text" name="last_name" value="{{ $student->last_name }}" class="form-control @error('last_name') is-invalid @enderror" id="inputName" placeholder="Last Name"> @error('last_name') <div class="form-text text-danger">{{ $message }}</div> @enderror </div> <div class="mb-3"> <label for="inputDetail" class="form-label"><strong>Address:</strong></label> <textarea class="form-control @error('address') is-invalid @enderror" style="height:150px" name="address" id="inputDetail" placeholder="Address">{{ $student->address }}</textarea> @error('address') <div class="form-text text-danger">{{ $message }}</div> @enderror </div> <button type="submit" class="btn btn-success"><i class="fa-solid fa-floppy-disk"></i> Update</button> </form> </div> </div> @endsection |
resources/views/students/show.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 | @extends('students.layout') @section('content') <div class="card mt-5"> <h2 class="card-header">Show Student</h2> <div class="card-body"> <div class="d-grid gap-2 d-md-flex justify-content-md-end"> <a class="btn btn-primary btn-sm" href="{{ route('students.index') }}"><i class="fa fa-arrow-left"></i> Back</a> </div> <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>First Name:</strong> <br/> {{ $student->first_name }} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Last Name:</strong> <br/> {{ $student->last_name }} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12 mt-2"> <div class="form-group"> <strong>Address:</strong> <br/> {{ $student->address }} </div> </div> </div> </div> </div> @endsection |
Step 9: Run Laravel Application
We can start the server and run this example using the below command.
1 | php artisan serve |
Now we will run our example using the below Url in the browser.
1 | http://127.0.0.1:8000/students |
Download laravel 11 CRUD example from github