Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Laravel 5 Essentials

You're reading from   Laravel 5 Essentials Explore the fundamentals of Laravel, one of the most expressive and robust PHP frameworks available

Arrow left icon
Product type Paperback
Published in Apr 2015
Publisher Packt
ISBN-13 9781785283017
Length 144 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Martin Bean Martin Bean
Author Profile Icon Martin Bean
Martin Bean
Arrow right icon
View More author details
Toc

Deleting data


There are two ways of deleting records. If you have a model instance that you have fetched from the database, then you can call the delete method on it:

$cat = Cat::find(1);
$cat->delete();

Alternatively, you can call the destroy method, specifying the IDs of the records you want to delete, without having to fetch those records first:

Cat::destroy(1);
Cat::destroy(1, 2, 3, 4, 5);

Soft deletion

By default, Eloquent will hard-delete records from your database. This means, once it's deleted, it's gone forever. If you need to retain deleted data (that is, for auditing), then you can use soft deletes. When deleting a model, the record is kept in the database but instead a deleted_at timestamp is set, and any records with this timestamp set will not be included when querying your database.

Soft deletes can be easily added to your Eloquent model. All you need to do is include the trait:

use Illuminate\Database\Eloquent\SoftDeletes;
class Cat extends Model {
  use SoftDeletes;
  protected...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image