I am trying to implement soft deleting concept.
Here is my object:
class Post extends Eloquent { /** * The database table used by the model. * * @var string */ protected $table = 'posts'; protected $softDelete = true; ...
Soft delete is on.
Now, if I 'delete' a post, it gets a 'deleted_at' timestamp:
The problem is, when I search or just use all()
to display the posts, the soft deleted items appears there. What is wrong?
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();
The soft deleting feature works when using Eloquent. If you are querying the results with query builder you will eventually see all the records trashed and not trashed.
It is not clear in the current docs of Laravel 4, but seeing that the concept of soft deleting just appears under Eloquent ORM - Soft Deleting and not under Query Builder, we can only assume that: soft delete only works with Eloquent ORM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With