Hello dev, Today we are going to learn How to Delete Multiple Data using Checkbox in Laravel 10. Deleting multiple records at once is a common requirement in web applications, especially when dealing with large datasets. Laravel, a popular PHP framework, provides powerful features to handle this task efficiently.
In this blog post, we will explore how to implement a checkbox-based multiple data deletion feature in Laravel 10.
Prerequisites:
To follow along with this tutorial, you should have a basic understanding of PHP, Laravel, and web development concepts. Make sure you have Laravel 10 installed on your development environment.
Step 1: Set up the Laravel Project
Create a new Laravel project by running the following command in your terminal:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Create the Database and Migration
Next, create a database for your project and update the .env
file with the appropriate database credentials. Once the database is set up, create a migration using the following command:
php artisan make:migration create_posts_table --create=posts
Open the generated migration file located at database/migrations
and define the table schema. For example:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
});
}
Run the migration to create the posts
table in your database:
php artisan migrate
Step 3: Create the Model and Controller
Generate a model and a controller for the Post
resource using the following commands:
php artisan make:model Post --migration
php artisan make:controller PostController --resource
Step 4: Define Routes
Open the routes/web.php
file and define the necessary routes for the Post
resource:
use App\Http\Controllers\PostController;
Route::resource('posts', PostController::class);
Step 5: Build the View
Create a new directory called posts
inside the resources/views
directory. Inside the posts
directory, create a new file called index.blade.php
.
Open the index.blade.php
file and add the following code:
@extends('layouts.app')
@section('content')
<form action="{{ route('posts.destroyMultiple') }}" method="POST">
@csrf
@method('DELETE')
<table>
<thead>
<tr>
<th>Select</th>
<th>Title</th>
<th>Content</th>
</tr>
</thead>
<tbody>
@foreach ($posts as $post)
<tr>
<td><input type="checkbox" name="ids[]" value="{{ $post->id }}"></td>
<td>{{ $post->title }}</td>
<td>{{ $post->content }}</td>
</tr>
@endforeach
</tbody>
</table>
<button type="submit">Delete Selected</button>
</form>
@endsection
Step 6: Implement Delete Functionality
Open the PostController
located at app/Http/Controllers/PostController.php
and add the following code:
public function destroyMultiple(Request $request)
{
$ids = $request->input('ids');
Post::whereIn('id', $ids)->delete();
return redirect()->route('posts.index')->with('success', 'Selected posts have been deleted.');
}
Step 7: Update the Routes
Finally, open the routes/web.php
file and add the following route definition at the bottom:
Route::delete('posts/destroyMultiple', [PostController::class, 'destroyMultiple'])->name('posts.destroyMultiple');
Conclusion:
In this tutorial, we have covered the steps required to implement a checkbox-based multiple data deletion feature in Laravel 10. By following these steps, you should now have a fully functional system that allows users to select multiple records using checkboxes and delete them simultaneously.
This approach can significantly enhance the user experience when dealing with large datasets. Feel free to explore additional enhancements such as validation, confirmation dialogs, or pagination to further improve the feature.