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?
I had the same problem and nothing here helped me.
My problem was in my construct, I forgot to call the parent constructor:
Hope that help someone!
Sometimes, you will get the
soft deleted
table entries withget()
even with eloquent andprotected $softDelete = true;
.So to avoid this problem, use
For example, this query will fetch all rows including soft deleted.
So the proper method is,
Tested it in Laravel 5.6, You need to use SoftDeletes Trait in your model
and when you query
it will not return soft deleted data.
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.
There is a little trick using soft delete tables and queries in laravel:
When we create something like
The system executes something like that:
So far, so good. But, when we apply the "orWhere" method, something funny happens
The system will execute something like that:
This new query will return all the car where deleted_at is null and the color is blue OR if the color is red, even if the deleted_at is not null. It is the same behavior of this other query, what show the problem more explicitly:
To escape from this problem, you should change the "where" method passing a Closure. Like that:
Then, the system will execute something like that:
This last query, searches for all cars where deleted_at is null and where the color can be or red or blue, as we was want it to do.
I use
I works fine, I mean it doesn't return soft deleted items (items flagged with deleted_at timestamps). I am using Laravel 4.