Php 在使用自定义规则验证期间的更新函数中,忽略laravel中重复项的当前id
我使用的是Laravel6。我创建了一个表单来更新会议以及控制器中的一些验证规则,这些规则检查会议室当时是否空闲,以及参与者当时是否已在另一个会议中被占用。不幸的是,当我提交表格时,我只更改了会议的描述,验证过程无法正常工作,因为它表明当前会议室当时已被占用,而且参与者在另一次会议上也已被占用。。。这是因为验证过程不排除检查会议的当前id。因此,会议本身就是一个复制品 我尝试了很多方法来更改validate方法,不包括作为函数update_meeting的第二个参数传递的$id,但它们都不起作用 控制器: 自定义规则(检查室):Php 在使用自定义规则验证期间的更新函数中,忽略laravel中重复项的当前id,php,laravel,validation,controller,Php,Laravel,Validation,Controller,我使用的是Laravel6。我创建了一个表单来更新会议以及控制器中的一些验证规则,这些规则检查会议室当时是否空闲,以及参与者当时是否已在另一个会议中被占用。不幸的是,当我提交表格时,我只更改了会议的描述,验证过程无法正常工作,因为它表明当前会议室当时已被占用,而且参与者在另一次会议上也已被占用。。。这是因为验证过程不排除检查会议的当前id。因此,会议本身就是一个复制品 我尝试了很多方法来更改validate方法,不包括作为函数update_meeting的第二个参数传递的$id,但它们都不起作用
公共函数传递($attribute,$value)
{
$meetings=DB::表(‘会议’)
->其中('id_room',request('room'))
->其中(“日期”,请求(“会议日期”)
->其中(函数($query){
$query->where(函数($sub_q){
$sub_q->where('start_hour','>=',request('start'))
->其中('start_hour','=',request('end');
})
->orWhere(功能($sub_q){
$sub_q->where('end_hour','>',request('start'))
->其中('end_hour','在我的例子中,您可以这样使用它,我的字段是username和table name id admins,要忽略的id是$id
'username'=>'required|unique:admins,username,'.$id
您尝试过在构造函数中传递值吗?newcheckroom($id)
类检查室实现规则
{
受保护的$excludeId=null;
公共函数构造($id=null)
{
$this->excludeId=$id;
}
公共函数传递($attribute,$value)
{
返回数据库::表('会议')
...
->当($this->excludeId,函数($query,$exclude){
$query->where('id',''.$exclude);
})->doesntExist();
}
...
}
public function passes($attribute, $value)
{
$meetings = DB::table('meetings')
->where('id_room', request('room'))
->where('date', request('date_meeting'))
->where(function ($query) {
$query->where(function($sub_q) {
$sub_q->where('start_hour', '>=', request('start'))
->where('start_hour', '<', request('end'));
})
->orWhere(function($sub_q) {
$sub_q->where('start_hour', '<', request('start'))
->where('end_hour', '>=', request('end'));
})
->orWhere(function($sub_q) {
$sub_q->where('end_hour', '>', request('start'))
->where('end_hour', '<=', request('end'));
});
})->get();
if(count($meetings) > 0) {
return false;
} else {
return true;
}
}
'username'=>'required|unique:admins,username,'.$id
class CheckRoom implements Rule
{
protected $excludeId = null;
public function __construct($id = null)
{
$this->excludeId = $id;
}
public function passes($attribute, $value)
{
return DB::table('meetings')
...
->when($this->excludeId, function ($query, $exclude) {
$query->where('id', '<>', $exclude);
})->doesntExist();
}
...
}