【发布时间】:2018-10-29 14:20:25
【问题描述】:
我是 laravel 的新手。我有一个带有 menu_id 和标题的表,当具有相同的 menu_id 时,我试图使这个标题字段唯一。我找到了解决方案here
但是更新时遇到问题。有人可以帮忙吗? 我的代码
Validator::extend('unique_custom', function ($attribute, $value, $parameters)
{
// Get the parameters passed to the rule
list($table, $field, $field2, $field2Value) = $parameters;
// Check the table and return true only if there are no entries matching
// both the first field name and the user input value as well as
// the second field name and the second field value
return \DB::table($table)->where($field, $value)->where($field2, $field2Value)->count() == 0;
});
public function updateSubmenu( Request $request) {
$this->validate( $request, [
'menu_id' => 'required',
'title' => 'required|unique_custom:posts,title,menu_id,'.$request->menu_id,
'order_by' => 'required|integer',
'description' => 'required'
],
[
'title.unique_custom' => 'This title already token'
]
);
}
【问题讨论】:
标签: laravel-5.4 laravel-validation