soft-delete
Why soft deleted entities appear in query results?
Sometimes, you will get the soft deleted table entries with get() even with eloquent and protected $softDelete = true;. So to avoid this problem, use …->whereNull(‘deleted_at’)->get(); For example, this query will fetch all rows including soft deleted. DB::table(‘pages’)->select(‘id’,’title’, ‘slug’) ->where(‘is_navigation’,’=’,’yes’) ->where(‘parent_id’,’=’,$parent_id) ->orderBy(‘page_order’) ->get(); So the proper method is, DB::table(‘pages’)->select(‘id’,’title’, ‘slug’) ->where(‘is_navigation’,’=’,’yes’) ->where(‘parent_id’,’=’,$parent_id) ->whereNull(‘deleted_at’) ->orderBy(‘page_order’) ->get();
Cascading Soft Delete
I’ve come up with a solution to cascading soft-deletes recently using Postgres 9.6 that makes use of inheritance to partition entries into deleted and non-deleted ones. Here’s a copy of the document that I’m writing for our project: Cascading soft-deletes Abstract In this document I describe our current approach to deal with deletion of objects … Read more
Laravel Soft Delete restore() Error
Error says $post is a non-object, Laravel doesn’t return trashed records without withTrashed() Post::withTrashed()->find($post_id)->restore(); Laravel Docs – Soft Deleting When querying a model that uses soft deletes, the “deleted” models will not be included…
How to check if row is soft-deleted in Eloquent?
Just realised I was looking in the wrong API. The Model class doesn’t have this, but the SoftDelete trait that my models use has a trashed() method. So I can write if ($thing->trashed()) { … }
Are soft deletes a good idea? [duplicate]
I say it’s a bad idea, generally (with some exceptions, perhaps). First, your database should be backed up regularly, so you should never be in a situation where you would lose data permanently because of a DELETE (unless it’s a deletion of just-added data, of course). Second, a soft delete like this means you now … Read more
Physical vs. logical (hard vs. soft) delete of database record? [closed]
Advantages are that you keep the history (good for auditing) and you don’t have to worry about cascading a delete through various other tables in the database that reference the row you are deleting. Disadvantage is that you have to code any reporting/display methods to take the flag into account. As far as if it … Read more