Laravel provides a clean and fluent API for building database queries using Eloquent ORM or the Query Builder. When it comes to deleting records from your database, Laravel makes it very straightforward.
In this blog, we’ll explore how to use the Laravel Query Builder to delete records with real examples.
Query Builder is a lightweight and fast way to perform database operations directly without loading models.
Syntax :-
DB::table('table_name')->where('condition')->delete();
Example 1: Delete a Blog Post by ID.
DB::table('blogs')->where('id', 5)->delete();
Example 2: Bulk Delete Based on a Condition.
DB::table('blogs')->where('status', 'draft')->delete();
Eloquent is Laravel’s ActiveRecord implementation and provides a model-centric approach, with support for soft deletes, model events, and relationships.
Syntax :-
Model::find($id)->delete();
Example 1: Delete a Blog Post by ID
use App\Models\Blog;
$blog = Blog::find(5);
$blog->delete();
Example 2: Bulk Delete with Conditions
Blog::where('author_id', 3)->where('status', 'archived')->delete();
use Illuminate\Database\Eloquent\SoftDeletes;
class Blog extends Model{
use SoftDeletes;
}
Use Query Builder for fast and simple deletes, especially for bulk operations.
Choose Eloquent when you need soft deletes, relationships, or model events.
Pick the method that fits your use case – performance or model-driven logic.
And always handle deletes with care, especially in production!
Work with our skilled Laravel developers to accelerate your project and boost its performance.
Hire Laravel Developer