Laravel,无法更新软删除的值 [英] Laravel, Can't update a soft-deleted value
问题描述
我的数据库中有以下值:
I have this values in my db:
id - name - created_at - updated_at - deleted_at
------------------------------------------------
1 - John - 2018-11-11 - 2018-11-11 - (NULL)
2 - John - 2018-11-11 - 2018-11-11 - 2018-11-11
如果我使用数据表(Yajra)搜索"John",我只会看到带有id=1
的John,因为我正在使用软删除.我的模型是这样的:
If I search for "John" with my Datatable (Yajra) I only see the John with id=1
because I'm using softdeletes. My model is this:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class MyModel extends Model
{
use SoftDeletes;
protected $fillable = ['name'];
protected $dates = ['deleted_at'];
}
当我删除(销毁)注册表时,它会在deleted_at
上放置一个正确的日期.但是,当我要编辑(更新)John
时,验证程序给我的错误是该值已被使用.我的更新方法是这样:
When I delete (destroy) a registry it puts a date at deleted_at
which is correct. But when I want to edit (update) John
the Validator is giving me the error that that value is already in use. My update method is this:
public function update(Request $request, $id)
{
$rules = array(
'name' => 'unique:my_table'
);
$validator = Validator::make($request->all(), $rules);
if ($validator->passes()) {
MyModel::find($id)->update($request->input());
return redirect()->route('myroute')->withFlashSuccess('Ok!');
} else {
return redirect()->back()->withInput()->withErrors($validator);
}
}
我做错了什么?
推荐答案
该问题与SoftDeletes
无关,它是一个验证问题. unique
验证规则非常特殊,因为在进行更新的情况下,它需要知道执行验证时可能忽略的条目.在后台,规则正在执行SQL查询,例如
The problem has nothing to do with the SoftDeletes
, it is a validation issue. The unique
validation rule is very special, because in case of an update it needs to know which entry it may ignore when performing the validation. In the background, the rule is performing an SQL query like
IF EXISTS (
SELECT id
FROM my_table
WHERE name = 'some value'
)
SELECT 1
ELSE
SELECT 0
(它可能不是确切的查询,但类似).
(it may not be the exact query, but similar).
如您所见,无论是否执行更新,查询都不会考虑.因为您的实体已经存在,所以它将返回1
,因此由于验证认为所验证的值不是唯一的而失败,从而导致验证失败.
As you can see, the query does not take into account if you perform an update or not. Because your entity already exists, it will return 1
and therefore fail the validation because it thinks that the value under validation is not unique.
但是实际上有一种方法可以使验证适用于更新.您只需要将现有实体(正在验证)的id
作为第三个参数添加到验证规则.因此,您的规则应如下所示:
But there is actually a way to make the validation work for updates. You simply have to add the id
of the existing entity (which is being validated) as third parameter to the validation rule. So your rules should look something like this:
$rules = [
'name' => 'unique:my_table,name,'.$id
];
请注意,unique
验证规则还有另一个参数-您要搜索的数据库表的列.
Please be aware that there is also a second parameter for the unique
validation rule - the column of the database table you want to search in.
如果唯一约束仅与未删除条目有关,则意味着如果其他相同值的事件被标记为已删除,则可以重用唯一值,则可能需要添加unique
验证规则的附加where()
子句.为此,需要将第四个参数设置为identifier
列名,然后我们可以将附加的where
子句添加为两个参数对.
In case the unique constraint does only relate to not deleted entries, meaning that a unique value may be reused if other occurences of the same value are flagged as deleted, then it may be necessary to add an additional where()
clause to the unique
validation rule. For this, the fourth parameter needs to be set to the identifier
column name and then we can add the additional where
clauses as pairs of two parameters.
$rules = [
'name' => 'unique:my_table,name,'.$id.',id,deleted_at,NULL'
];
这会将where('deleted_at', 'NULL')
(或whereNull('deleted_at')
)添加到查询中.
This will add where('deleted_at', 'NULL')
(or whereNull('deleted_at')
) to the query.
这篇关于Laravel,无法更新软删除的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!