我正在尝试将存储功能()适应我的update()方法,我认为问题出在我身上吗?
在我的函数store()中,我有这个:(确定)
$date_start = $request->get('date_seance');
$hour_start = $request->get('hour_start');
$hour_end = $request->get('hour_end');
$fk_motorbike = $request->get('fk_motorbike');
$fk_former = $request->get('fk_former');
$conflictTraining = Training::where('fk_motorbike', $fk_motorbike)
->whereDate('date_seance', "=" , $date_start)
->where('hour_start', "<=" , $request->get('hour_start'))
->where('hour_end', ">=" , $request->get('hour_end'))
->where('fk_former', $request->get('fk_former'))
->where('fk_motorbike', $request->get('fk_motorbike'))
->first();
$conflictRevision = Revision::where('fk_motorbike', $fk_motorbike)
->whereDate('date_revision_start', "<=" , $date_start)
->whereDate('date_revision_end', ">=", $date_start)
->where('hour_start', "<=" , $request->get('hour_start'))
->where('hour_end', ">=" , $request->get('hour_end'))
->first();
$conflictFormer = Training::where('fk_former', $fk_former)
->whereDate('date_seance', "=" , $date_start)
->where('hour_start', "<=" , $request->get('hour_start'))
->where('hour_end', ">=" , $request->get('hour_end'))
->where('fk_former', $request->get('fk_former'))
->first();
$conflictMotorbike = Training::where('fk_motorbike', $fk_motorbike)
->whereDate('date_seance', "=" , $date_start)
->where('hour_start', "<=" , $request->get('hour_start'))
->where('hour_end', ">=" , $request->get('hour_end'))
->where('fk_motorbike', $request->get('fk_motorbike'))
->first();
if(isset($conflictTraining) || isset($conflictRevision) || isset($conflictFormer) || isset($conflictMotorbike) ){
return redirect()->route('trainings.index')
->with('error', 'Duplicate ');
}
else{
Training::create($request->all());
return redirect()->route('trainings.index')
->with('success', 'Add');
}
在我的函数update()中,我有这个:
$date_start = $request->get('date_seance');
$hour_start = $request->get('hour_start');
$hour_end = $request->get('hour_end');
$fk_motorbike = $request->get('fk_motorbike');
$fk_former = $request->get('fk_former');
$conflictTraining = Training::where('fk_motorbike', $fk_motorbike)
->whereDate('date_seance', "=" , $date_start)
->where('hour_start', "<=" , $request->get('hour_start'))
->where('hour_end', ">=" , $request->get('hour_end'))
->where('fk_former', $request->get('fk_former'))
->where('fk_motorbike', $request->get('fk_motorbike'))
->first();
$conflictRevision = Revision::where('fk_motorbike', $fk_motorbike)
->whereDate('date_revision_start', "<=" , $date_start)
->whereDate('date_revision_end', ">=", $date_start)
->where('hour_start', "<=" , $request->get('hour_start'))
->where('hour_end', ">=" , $request->get('hour_end'))
->first();
$conflictFormer = Training::where('fk_former', $fk_former)
->whereDate('date_seance', "=" , $date_start)
->where('hour_start', "<=" , $request->get('hour_start'))
->where('hour_end', ">=" , $request->get('hour_end'))
->where('fk_former', $request->get('fk_former'))
->first();
$conflictMotorbike = Training::where('fk_motorbike', $fk_motorbike)
->whereDate('date_seance', "=" , $date_start)
->where('hour_start', "<=" , $request->get('hour_start'))
->where('hour_end', ">=" , $request->get('hour_end'))
->where('fk_motorbike', $request->get('fk_motorbike'))
->first();
if( (isset($conflictTraining) && $conflictTraining->id !== intval($id))
|| (isset($conflictRevision) && $conflictRevision->id !== intval($id))
|| (isset($conflictFormer) && $conflictFormer->id !== intval($id))
|| (isset($conflictMotorbike) && $conflictMotorbike->id !== intval($id)) ){
return redirect()->route('trainings.index')
->with('error', 'Duplicate! ');
else{
$trainings = Training::find($id);
$trainings->date_start = $request->get('date_seance');
$trainings->hour_start = $request->get('hour_start');
$trainings->hour_end = $request->get('hour_end');
$trainings->fk_motorbike = $request->get('fk_motorbike');
$trainings->fk_former = $request->get('fk_former');
$trainings->save();
return redirect()->route('trainings.index')
->with('success', 'update')->withInput();
}
}
}
我认为问题出在这里???我的情况不正确??
if( (isset($conflictTraining) && $conflictTraining->id !== intval($id))
|| (isset($conflictRevision) && $conflictRevision->id !== intval($id))
|| (isset($conflictFormer) && $conflictFormer->id !== intval($id))
|| (isset($conflictMotorbike) && $conflictMotorbike->id !== intval($id)) ){
return redirect()->route('trainings.index')
->with('error', 'Duplicate! ');
如果您有任何想法,请不要犹豫...
感谢您的帮助和解释。
答案 0 :(得分:1)
是的,问题出在有条件的问题上。或至少是部分问题。
您要检查的$id
(来自表单)仅对一件事有用:培训模型。因此,检查id
是否与冲突模型(修订版,摩托车,前者)相匹配,失败或不正确。 (用于培训的id
与以上任何模型都不匹配)。检验id
的唯一原因是确保您的重复培训与来自表单的培训不同-IE如果相同,则应允许对其进行编辑,并且没有标记为重复。其他检查是查看是否存在合法重复。
要解决此问题,请在除培训模型之外的所有内容上取消id
匹配项的检查。这样的事情(只需手动执行,您就需要仔细检查代码)。
视情况而定:
if( (isset($conflictTraining) && $conflictTraining->id !== intval($id))
|| isset($conflictRevision)
|| isset($conflictFormer)
|| isset($conflictMotorbike) // <-- Don't need this or its query per edit
){
return redirect()->route('trainings.index')
->with('error', 'Duplicate! ');
所有这些都说明,这种方法变得非常强大/复杂。您可能希望考虑限制其中一些,或者将一些繁重的工作带入另一种方法或领域。一旦启动并运行它,您可能还希望缩减一些代码以使其更高效。
查询
也-$conflictFormer
上的查询有问题。 fk_former
检查在查询中两次。嗯,看来您的几个查询在FK上有多个where
语句可以删除。
再仔细一点,其中一些“美”变得越来越复杂。通过这样运行您的训练查询:
$conflictTraining = Training::where('fk_motorbike', $fk_motorbike)
->whereDate('date_seance', "=" , $date_start)
->where('hour_start', "<=" , $request->get('hour_start'))
->where('hour_end', ">=" , $request->get('hour_end'))
->where('fk_former', $request->get('fk_former')) // <-- only if same former
->where('fk_motorbike', $request->get('fk_motorbike')) // <-- repeated code
->first();
这不是检查任何训练是否有冲突,而是检查训练是否与特定的前者有冲突。我将在这里删除最后两个where
语句。您稍后再检查是否有冲突。
最后一个查询$conflictMotorbike
是不必要的,因为第一个关于训练冲突的查询已经告诉您这一点,因为您在查询的最初部分检查了摩托车。建议您删除它,并isset
检查$conflictMotorbike
。