To delete a record in Laravel, you can use the delete
method on a model instance. For example:
$post = App\Post::find(1);
$post->delete();
This will delete the Post
model with an id
of 1.
If you want to delete multiple records at once, you can use the destroy
method on the model’s query builder. For example:
App\Post::destroy(1, 2, 3);
This will delete the Post
models with an id
of 1, 2, and 3.
You can also use the destroy
method with a collection of model instances:
$posts = App\Post::where('active', 0)->get();
App\Post::destroy($posts);
This will delete all Post
models that have an active
column with a value of 0.
Keep in mind that these methods will permanently delete the records from the database. If you want to “soft delete” a model, meaning that it is not actually deleted from the database but a deleted_at
timestamp is set, you can use the softDelete
method provided by Laravel’s built-in soft delete feature.