On this tutorial, we’ll learn to create a CRUD Operation utilizing Laravel 11.
Step 1: Set up Laravel
composer create–venture laravel/laravel crud–app |
Step 2: Set Up Your Setting
Be certain that your MySQL database is correctly configured. In your .env file, configure the database connection:
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravelcruddb DB_USERNAME=root DB_PASSWORD= |
Step 3: Create the MySQL desk (customers)
Desk construction for desk customers
CREATE TABLE `customers` ( `id` int(11) NOT NULL, `firstName` varchar(255) DEFAULT NULL, `lastName` varchar(255) DEFAULT NULL, `emailId` varchar(255) DEFAULT NULL, `phoneNumber` bigint(12) DEFAULT NULL, `streetAddress` textual content DEFAULT NULL, `zipCode` int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; |
Step 4:Create a balde file for knowledge insertion
replace.blade.php (assets/views/insert.blade.php)
<type motion=”https://phpgurukul.com/laravel-11-crud-operation/{{ route(“adduser’) }}” methodology=”submit”>
@csrf
<div class=”formbold-form-title”>
<h2 class=””>Register now ———– <small>
<a href=”https://phpgurukul.com/laravel-11-crud-operation/{{ route(“learn’) }}”> View Information</a>
</small> </h2>
<hr />
<ul model=”shade:purple; margin-top:2%” >
@if($errors->all())
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
@endif
</ul>
</div>
<div class=”formbold-input-flex”>
<div>
<label for=”firstname” class=”formbold-form-label”>
First title
</label>
<enter
sort=”textual content”
title=”firstname”
id=”firstname”
worth=”{{ previous(‘firstname’) }}”
class=”formbold-form-input @error(‘firstname’) is-invalid @enderror”
/>
</div>
<div>
<label for=”lastname” class=”formbold-form-label”> Final title </label>
<enter
sort=”textual content”
title=”lastname”
id=”lastname”
class=”formbold-form-input”
/>
</div>
</div>
<div class=”formbold-input-flex”>
<div>
<label for=”e-mail” class=”formbold-form-label”> Electronic mail </label>
<enter
sort=”e-mail”
title=”e-mail”
id=”e-mail”
class=”formbold-form-input”
/>
</div>
<div>
<label for=”cellphone” class=”formbold-form-label”> Telephone quantity </label>
<enter
sort=”textual content”
title=”cellphone”
id=”cellphone”
class=”formbold-form-input”
/>
</div>
</div>
<div class=”formbold-mb-3″>
<label for=”handle” class=”formbold-form-label”>
Avenue Deal with
</label>
<enter
sort=”textual content”
title=”handle”
id=”handle”
class=”formbold-form-input”
/>
</div>
<div class=”formbold-input-flex”>
<div>
<label for=”submit” class=”formbold-form-label”> Put up/Zip code </label>
<enter
sort=”textual content”
title=”submit”
id=”submit”
class=”formbold-form-input”
/>
</div>
</div>
<button class=”formbold-btn” sort=”submit”>Register Now</button>
</type>
</div>
</div>
<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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
<div class=“formbold-main-wrapper”> <div class=“formbold-form-wrapper”>
<type motion=“https://phpgurukul.com/laravel-11-crud-operation/{{ route(“adduser’) }}” methodology=“submit”> @csrf <div class=“formbold-form-title”> <h2 class=“”>Register now —————– <small> <a href=“https://phpgurukul.com/laravel-11-crud-operation/{{ route(“learn’) }}”> View Information</a> </small> </h2> <hr /> <ul model=“shade:purple; margin-top:2%” > @if($errors->all()) @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach @endif </ul>
</div>
<div class=“formbold-input-flex”> <div> <label for=“firstname” class=“formbold-form-label”> First title </label> <enter sort=“textual content” title=“firstname” id=“firstname” worth=“{{ previous(‘firstname’) }}” class=“formbold-form-input @error(‘firstname’) is-invalid @enderror” /> </div>
<div> <label for=“lastname” class=“formbold-form-label”> Final title </label> <enter sort=“textual content” title=“lastname” id=“lastname” class=“formbold-form-input”
/> </div>
</div>
<div class=“formbold-input-flex”> <div> <label for=“e-mail” class=“formbold-form-label”> Electronic mail </label> <enter sort=“e-mail” title=“e-mail” id=“e-mail” class=“formbold-form-input” /> </div>
<div> <label for=“cellphone” class=“formbold-form-label”> Telephone quantity </label> <enter sort=“textual content” title=“cellphone” id=“cellphone” class=“formbold-form-input” /> </div> </div>
<div class=“formbold-mb-3”> <label for=“handle” class=“formbold-form-label”> Avenue Deal with </label> <enter sort=“textual content” title=“handle” id=“handle” class=“formbold-form-input” /> </div>
<div class=“formbold-input-flex”> <div> <label for=“submit” class=“formbold-form-label”> Put up/Zip code </label> <enter sort=“textual content” title=“submit” id=“submit” class=“formbold-form-input” /> </div>
</div>
<button class=“formbold-btn” sort=“submit”>Register Now</button> </type> </div> </div> <model> |
Step 5: Create a Mannequin and migration
Create a mannequin that can correspond to the database desk you wish to insert knowledge into. You are able to do this utilizing the artisan command:
//for this tutorial
php artisan make:mannequin person
pphp artisan make:mannequin your_ModelName //for this tutorial php artisan make:mannequin person
|
You outline the desk title and fields which might be mass assignable (e.g., firstname
, LastName, emailId
) on this mannequin.
app/Fashions/Customers.php
return new class extends Migration
{
/**
* Run the migrations.
*/
public operate up(): void
{
Schema::create(‘customers’, operate (Blueprint $desk) {
$table->id();
$table->string(‘firstName’,200);
$table->string(‘lastName’,200)->nullable();
$table->string(’emailId’,’200′);
$table->bigInteger(‘phoneNumber’)->unsigned();
$table->textual content(‘streetAddress’);
$table->integer(‘zipCode’)->unsigned();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public operate down(): void
{
Schema::dropIfExists(‘customers’);
}
};
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 |
<?php use IlluminateDatabaseMigrationsMigration; use IlluminateDatabaseSchemaBlueprint; use IlluminateAssistFacadesSchema;
return new class extends Migration { /** * Run the migrations. */ public operate up(): void { Schema::create(‘customers’, operate (Blueprint $desk) { $desk->id(); $desk->string(‘firstName’,200); $desk->string(‘lastName’,200)->nullable(); $desk->string(’emailId’,‘200’); $desk->bigInteger(‘phoneNumber’)->unsigned(); $desk->textual content(‘streetAddress’); $desk->integer(‘zipCode’)->unsigned(); $desk->timestamps(); }); }
/** * Reverse the migrations. */ public operate down(): void { Schema::dropIfExists(‘customers’); } };
|
Step 6: Create a controller for dealing with person inputs and insert/learn/replace/delete the information into the database.
php artisan make:controller your_contollername //For this tutorial php artisan make:controller crudController
|
app/Http/Controllers/crudController.php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateSupportFacadesDB;
use AppHttpModelsUser;
class crudController extends Controller
{
public operate addUser(Request $request){
$request->validate([
‘firstname’ => ‘required’,
‘lastname’ => ‘required’,
’email’ => ‘required’,
‘phone’ => ‘required|numeric’,
‘address’ => ‘required’,
‘post’ => ‘required’
]);
$person=DB::desk(‘customers’)
->insert([
‘firstName’ => $request->firstname,
‘lastName’ => $request->lastname,
’emailId’ => $request->email,
‘phoneNumber’ => $request->phone,
‘streetAddress’ => $request->address,
‘zipCode’ => $request->post,
]);
if($person){
return redirect()->route(‘learn’)->with(‘success’, ‘Information inserted efficiently.’);
} else{
return redirect()->route(‘learn’)->with(‘error’, ‘One thing went flawed . Please Attempt once more.’);
}
}
}
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 |
<?php
namespace AppHttpControllers; use IlluminateHttpRequest; use IlluminateAssistFacadesDB; use AppHttpFashionsConsumer;
class crudController extends Controller { public operate addUser(Request $request){ $request->validate([ ‘firstname’ => ‘required’, ‘lastname’ => ‘required’, ’email’ => ‘required’, ‘phone’ => ‘required|numeric’, ‘address’ => ‘required’, ‘post’ => ‘required’
]); $person=DB::desk(‘customers’) ->insert([ ‘firstName’ => $request->firstname, ‘lastName’ => $request->lastname, ’emailId’ => $request->email, ‘phoneNumber’ => $request->phone, ‘streetAddress’ => $request->address, ‘zipCode’ => $request->post,
]);
if($person){ return redirect()->route(‘learn’)->with(‘success’, ‘Information inserted efficiently.’); } else{ return redirect()->route(‘learn’)->with(‘error’, ‘One thing went flawed . Please Attempt once more.’); } } } |
Step7: Add a Path to Deal with the Type Submission
use AppHttpControllerscrudController;
use IlluminateSupportFacadesRoute;
Route::get(‘/’, operate () {
return view(‘insert’);
});
Route::controller(crudController::class)->group(operate (){
Route::submit(‘/adduser’,’addUser’)->title(‘adduser’);
});
<?php
use AppHttpControllerscrudController; use IlluminateAssistFacadesRoute;
Route::get(‘/’, operate () { return view(‘insert’); }); Route::controller(crudController::class)->group(operate (){ Route::submit(‘/adduser’,‘addUser’)->title(‘adduser’); }); |
Step 8: We’ve got already created crudController; now, we’ll add the operate to point out the inserted knowledge in that controller.
public operate showUser(){ $customers=DB::desk(‘customers’) ->paginate(5); return view(‘learn’,[‘data’ => $users]); } |
Step 9: Create a blade template to show/present knowledge.
replace.blade.php (assets/views/learn.blade.php)
@if(session(‘error’))
<div class=”alert alert-danger”>
{{ session(‘error’) }}
</div>
@endif
<desk class=”desk table-bordered”>
<thead>
<tr>
<th>First Title</th>
<th>Final NAme</th>
<th width=”200″>Electronic mail id</th>
<th>Telephone No</th>
<th>Deal with</th>
<th>Zip Code</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach ($knowledge as $id => $person )
<tr>
<td>{{ $user->firstName }}</td>
<td>{{ $user->lastName }}</td>
<td>{{ $user->emailId }}</td>
<td>{{ $user->phoneNumber }}</td>
<td>{{ $user->streetAddress }}</td>
<td>{{ $user->zipCode }}</td>
<td>
<a category=”add” title=”Add” data-toggle=”tooltip”><i class=”material-icons”></i></a>
<a category=”edit” title=”Edit” data-toggle=”tooltip” href=”https://phpgurukul.com/laravel-11-crud-operation/{{ route(“replace’, $user->id) }}”><i class=”material-icons”></i></a>
<a category=”delete” title=”Delete” data-toggle=”tooltip” href=”https://phpgurukul.com/laravel-11-crud-operation/{{ route(“delete’, $user->id) }}”><i class=”material-icons”></i></a>
</td>
</tr>
@endforeach
</tbody>
</desk>
<div class=”mt-5″>
{{ $data->hyperlinks() }}
</div>
<div class=”mt-5″>
Whole Customers: {{ $data->complete()}}
</div>
</div>
</div>
</div>
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 |
<div class=“container-lg”> <div class=“table-responsive”> <div class=“table-wrapper”> <div class=“table-title”> <div class=“row”> <div class=“col-sm-8”><h2>Consumer <b>Particulars</b></h2></div> <div class=“col-sm-4”> <a class=“btn btn-info add-new” href=“https://phpgurukul.com/laravel-11-crud-operation/{{ route(“insert’) }}”><i class=“fa fa-plus”></i> Add New</a> </div> </div> </div> @if(session(‘success’)) <div class=“alert alert-success”> {{ session(‘success’) }} </div> @endif
@if(session(‘error’)) <div class=“alert alert-danger”> {{ session(‘error’) }} </div> @endif <desk class=“desk table-bordered”> <thead> <tr> <th>First Title</th> <th>Final NAme</th> <th width=“200”>Electronic mail id</th> <th>Telephone No</th> <th>Deal with</th> <th>Zip Code</th> <th>Actions</th> </tr> </thead> <tbody> @foreach ($knowledge as $id => $person )
<tr> <td>{{ $person->firstName }}</td> <td>{{ $person->lastName }}</td> <td>{{ $person->emailId }}</td> <td>{{ $person->phoneNumber }}</td> <td>{{ $person->streetAddress }}</td> <td>{{ $person->zipCode }}</td> <td> <a class=“add” title=“Add” knowledge–toggle=“tooltip”><i class=“material-icons”></i></a> <a class=“edit” title=“Edit” knowledge–toggle=“tooltip” href=“https://phpgurukul.com/laravel-11-crud-operation/{{ route(“replace’, $user->id) }}”><i class=“material-icons”></i></a> <a class=“delete” title=“Delete” knowledge–toggle=“tooltip” href=“https://phpgurukul.com/laravel-11-crud-operation/{{ route(“delete’, $user->id) }}”><i class=“material-icons”></i></a> </td> </tr> @endforeach </tbody> </desk> <div class=“mt-5”> {{ $knowledge->hyperlinks() }} </div> <div class=“mt-5”> Whole Customers: {{ $knowledge->complete()}} </div> </div> </div> </div> |
Step 10: Add the routes to the routes/internet.php
use AppHttpControllerscrudController;
use IlluminateSupportFacadesRoute;
Route::get(‘/’, operate () {
return view(‘insert’);
});
Route::controller(crudController::class)->group(operate (){
Route::submit(‘/adduser’,’addUser’)->title(‘adduser’);
Route::get(‘/learn’,’showUser’)->title(‘learn’);
});
<?php
use AppHttpControllerscrudController; use IlluminateAssistFacadesRoute;
Route::get(‘/’, operate () { return view(‘insert’); }); Route::controller(crudController::class)->group(operate (){ Route::submit(‘/adduser’,‘addUser’)->title(‘adduser’); Route::get(‘/learn’,‘showUser’)->title(‘learn’); }); |
Step 11: Create the Edit View
replace.blade.php (assets/views/replace.blade.php)
<div class=”formbold-form-wrapper”>
<type motion=”https://phpgurukul.com/laravel-11-crud-operation/{{ route(“updateuser’, $data->id ) }}” methodology=”submit”>
@csrf
<div class=”formbold-form-title”>
<h2 class=””>Replace Consumer Information</h2>
</div>
@if(session(‘success’))
<div>{{ session(‘success’) }}</div>
@endif
<div class=”formbold-input-flex”>
<div>
<label for=”firstname” class=”formbold-form-label”>
First title
</label>
<enter
sort=”textual content”
title=”firstname”
id=”firstname”
class=”formbold-form-input”
worth=”{{ $data->firstName }}”
/>
</div>
<div>
<label for=”lastname” class=”formbold-form-label”> Final title </label>
<enter
sort=”textual content”
title=”lastname”
id=”lastname”
class=”formbold-form-input”
worth=”{{ $data->lastName }}”
/>
</div>
</div>
<div class=”formbold-input-flex”>
<div>
<label for=”e-mail” class=”formbold-form-label”> Electronic mail </label>
<enter
sort=”e-mail”
title=”e-mail”
id=”e-mail”
class=”formbold-form-input”
worth=”{{ $data->emailId }}”
/>
</div>
<div>
<label for=”cellphone” class=”formbold-form-label”> Telephone quantity </label>
<enter
sort=”textual content”
title=”cellphone”
id=”cellphone”
class=”formbold-form-input”
worth=”{{ $data->phoneNumber }}”
/>
</div>
</div>
<div class=”formbold-mb-3″>
<label for=”handle” class=”formbold-form-label”>
Avenue Deal with
</label>
<enter
sort=”textual content”
title=”handle”
id=”handle”
class=”formbold-form-input”
worth=”{{ $data->streetAddress }}”
/>
</div>
<div class=”formbold-input-flex”>
<div>
<label for=”submit” class=”formbold-form-label”> Put up/Zip code </label>
<enter
sort=”textual content”
title=”submit”
id=”submit”
class=”formbold-form-input”
worth=”{{ $data->zipCode }}”
/>
</div>
</div>
<button class=”formbold-btn” sort=”submit”>Replace</button>
</type>
</div>
</div>
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
<div class=“formbold-form-wrapper”> <type motion=“https://phpgurukul.com/laravel-11-crud-operation/{{ route(“updateuser’, $data->id ) }}” methodology=“submit”> @csrf <div class=“formbold-form-title”> <h2 class=“”>Replace Consumer Information</h2>
</div> @if(session(‘success’)) <div>{{ session(‘success’) }}</div> @endif <div class=“formbold-input-flex”> <div> <label for=“firstname” class=“formbold-form-label”> First title </label> <enter sort=“textual content” title=“firstname” id=“firstname” class=“formbold-form-input” worth=“{{ $data->firstName }}” /> </div> <div> <label for=“lastname” class=“formbold-form-label”> Final title </label> <enter sort=“textual content” title=“lastname” id=“lastname” class=“formbold-form-input” worth=“{{ $data->lastName }}” /> </div> </div>
<div class=“formbold-input-flex”> <div> <label for=“e-mail” class=“formbold-form-label”> Electronic mail </label> <enter sort=“e-mail” title=“e-mail” id=“e-mail” class=“formbold-form-input” worth=“{{ $data->emailId }}” /> </div> <div> <label for=“cellphone” class=“formbold-form-label”> Telephone quantity </label> <enter sort=“textual content” title=“cellphone” id=“cellphone” class=“formbold-form-input” worth=“{{ $data->phoneNumber }}” /> </div> </div>
<div class=“formbold-mb-3”> <label for=“handle” class=“formbold-form-label”> Avenue Deal with </label> <enter sort=“textual content” title=“handle” id=“handle” class=“formbold-form-input” worth=“{{ $data->streetAddress }}” /> </div>
<div class=“formbold-input-flex”> <div> <label for=“submit” class=“formbold-form-label”> Put up/Zip code </label> <enter sort=“textual content” title=“submit” id=“submit” class=“formbold-form-input” worth=“{{ $data->zipCode }}” /> </div>
</div> <button class=“formbold-btn” sort=“submit”>Replace</button> </type> </div> </div> |
Step 12: Create the operate to fetch/learn the actual person’s knowledge within the crudController
public operate updatePage(string $id){ //$customers=DB::desk(‘customers’)->the place(‘id’ , $id)->get(); $customers=DB::desk(‘customers’)->discover($id); return view(‘replace’,[‘data’ => $users]); }
|
Step 13: Create the operate to replace the actual person’s knowledge within the crudController.
]);
if($person){
return redirect()->route(‘learn’)->with(‘success’, ‘Information up to date efficiently.’);
// echo “<h2>Information Inserted Successfuly</h2>”;
} else{
return redirect()->route(‘learn’)->with(‘error’, ‘One thing went flawed . Please Attempt once more.’);
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public operate updateUser(Request $request, $id){ // return $request; $person=DB::desk(‘customers’) ->the place(‘id’, $id) ->replace([ ‘firstName’ => $request->firstname, ‘lastName’ => $request->lastname, ’emailId’ => $request->email, ‘phoneNumber’ => $request->phone, ‘streetAddress’ => $request->address, ‘zipCode’ => $request->post
]);
if($person){ return redirect()->route(‘learn’)->with(‘success’, ‘Information up to date efficiently.’); // echo “<h2>Information Inserted Successfuly</h2>”;
} else{ return redirect()->route(‘learn’)->with(‘error’, ‘One thing went flawed . Please Attempt once more.’);
} } |
Step 14: Outline Routes for replace.
Route::submit(‘/adduser’,’addUser’)->title(‘adduser’);
Route::get(‘/learn’,’showUser’)->title(‘learn’);
Route::get(‘/replace/{id}’,’updatePage’)->title(‘replace’);
Route::submit(‘/updateuser/{id}’,’updateUser’)->title(‘updateuser’);
Route::get(‘/delete/{id}’,’deleteUser’)->title(‘delete’);
});
<?php use AppHttpControllerscrudController; use IlluminateAssistFacadesRoute; Route::get(‘/’, operate () { return view(‘insert’); }); Route::controller(crudController::class)->group(operate (){
Route::submit(‘/adduser’,‘addUser’)->title(‘adduser’); Route::get(‘/learn’,‘showUser’)->title(‘learn’); Route::get(‘/replace/{id}’,‘updatePage’)->title(‘replace’); Route::submit(‘/updateuser/{id}’,‘updateUser’)->title(‘updateuser’); Route::get(‘/delete/{id}’,‘deleteUser’)->title(‘delete’); }); |
Step 15: Create the operate to delete the actual person’s knowledge within the crudController.
} else{
echo “<h2>One thing went flawed . Please Attempt once more. </h2>”;
}
}
public operate deleteUser(string $id){ $person=DB::desk(‘customers’) ->the place(‘id’, $id) ->delete(); if($person){ return redirect()->route(‘learn’)->with(‘success’, ‘Information deleted.’); // echo “<h2>Information Inserted Successfuly</h2>”;
} else{ echo “<h2>One thing went flawed . Please Attempt once more. </h2>”; } } |
Step 16: Outline Routes for deletion.
public operate deleteUser(string $id){ $person=DB::desk(‘customers’) ->the place(‘id’, $id) ->delete(); if($person){ return redirect()->route(‘learn’)->with(‘success’, ‘Information deleted.’); // echo “<h2>Information Inserted Successfuly</h2>”; } else{ echo “<h2>One thing went flawed . Please Attempt once more. </h2>”; } } |
The right way to run the Script
1. Obtain the venture zip file
2. Extract the file and replica crud-app
folder
3. Paste inside root listing (for xampp xampp/htdocs, for wamp wamp/www, for lamp var/www/Html)
4. Run these instructions
PS C :> cd xampp/htdocs/crud-app
PS C:xampphtdocscrud-app> php artisan migrate
PS C:xampphtdocscrud-app> php artisan serve
8. After that, open the browser and run the script
Script Demo
Laravel 11 CRUD Operation (Supply Code)
Measurement: 26.6 MB
Model: V 1.0