laravel 为什么软删除实体会出现在查询结果中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18041155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why soft deleted entities appear in query results?
提问by Sergey Sob
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:
现在,如果我“删除”一个帖子,它会得到一个“deleted_at”时间戳:
The problem is, when I search or just use all()
to display the posts, the soft deleted items appears there. What is wrong?
问题是,当我搜索或仅用于all()
显示帖子时,软删除的项目会出现在那里。怎么了?
采纳答案by Rubens Mariuzzo
The soft deletingfeature works when using Eloquent. If you are querying the results with query builderyou 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 deletingjust appears under Eloquent ORM - Soft Deletingand not under Query Builder, we can only assume that: soft delete only works with Eloquent ORM.
目前 Laravel 4 的文档中并不清楚,但是看到软删除的概念只出现在Eloquent ORM - Soft Deleting 下而不是 Query Builder 下,我们只能假设:软删除仅适用于 Eloquent ORM。
回答by devo
Sometimes, you will get the soft deleted
table entries with get()
even with eloquent and protected $softDelete = true;
.
有时候,你会得到的soft deleted
表项与get()
甚至口才和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();
回答by Thiago Mata
There is a little trick using soft delete tables and queries in laravel:
在 Laravel 中使用软删除表和查询有一个小技巧:
When we create something like
当我们创建类似
$objCars = Car::where("color","blue");
The system executes something like that:
系统执行如下操作:
SELECT
*
FROM
cars
WHERE
deleted_at IS NULL
AND
"color" = 'blue'
So far, so good. But, when we apply the "orWhere" method, something funny happens
到现在为止还挺好。但是,当我们应用“orWhere”方法时,会发生一些有趣的事情
$objCars = Car::where("color","blue")->orWhere("color","red");
The system will execute something like that:
系统将执行如下操作:
SELECT
*
FROM
cars
WHERE
deleted_at IS NULL
AND
"color" = 'blue'
OR
"color" = 'red'
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:
这个新查询将返回deleted_at 为空且颜色为蓝色或颜色为红色的所有汽车,即使deleted_at 不为空。这是另一个查询的相同行为,更明确地显示问题:
SELECT
*
FROM
cars
WHERE
(
deleted_at IS NULL
AND
"color" = 'blue'
)
OR
"color" = 'red'
To escape from this problem, you should change the "where" method passing a Closure. Like that:
为了避免这个问题,你应该改变传递闭包的“where”方法。像那样:
$objCars = Car::where(
function ( $query ) {
$query->where("color","blue");
$query->orWhere("color","red");
}
);
Then, the system will execute something like that:
然后,系统将执行如下操作:
SELECT
*
FROM
cars
WHERE
deleted_at IS NULL
AND
(
"color" = 'blue'
OR
"color" = 'red'
)
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.
最后一个查询,搜索所有deleted_at 为空并且颜色可以是红色或蓝色的汽车,正如我们希望它做的那样。
回答by William Perron
I had the same problem and nothing here helped me.
我遇到了同样的问题,这里没有任何帮助。
My problem was in my construct, I forgot to call the parent constructor:
我的问题出在我的构造中,我忘了调用父构造函数:
public function __construct()
{
parent::__construct();
//Rest of my code
}
Hope that help someone!
希望对某人有所帮助!
回答by Cholakov
Laravel 5.2.44 added withoutTrashed()
method to SoftDeletingScope
. For example you can use something like this:
Laravel 5.2.44withoutTrashed()
向SoftDeletingScope
. 例如,您可以使用以下内容:
Post::withoutTrashed()->get();
回答by RN Kushwaha
Tested it in Laravel 5.6, You need to use SoftDeletes Trait in your model
在 Laravel 5.6 中测试过,你需要在你的模型中使用 SoftDeletes Trait
use Illuminate\Database\Eloquent\SoftDeletes;
使用 Illuminate\Database\Eloquent\SoftDeletes;
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Banners extends Model
{
use SoftDeletes;
//no need of this below line
//protected $softDelete = true;
}
and when you query
当你查询时
$banners = Banners::where('status', 1)->get();
$banners = Banners::where('status', 1)->get();
it will not return soft deleted data.
它不会返回软删除的数据。
回答by Christopher Kikoti
I use
我用
Post::all()
I works fine, I mean it doesn't return soft deleted items (items flagged with deleted_at timestamps). I am using Laravel 4.
我工作正常,我的意思是它不会返回软删除的项目(用 delete_at 时间戳标记的项目)。我正在使用 Laravel 4。