【发布时间】:2019-09-09 01:44:41
【问题描述】:
编辑:(08/09/2019)
我有一个关于付款验证的问题。
例如,学生Leonard 支付09/09/2019 的培训仅。
在我的表格Training 中,我看到我可以添加几个降神会(培训)????
我可以阻止这个吗?
有关信息,在我的表 Training 中,我有 5 个外键 (fk_former, fk_student, fk_motorbike, fk_typetraining, fk_payment)。
要加入表培训和付款我这样做:
模型培训
class Training extends Model
{
//
protected $fillable = ['date_seance', 'hour_start', 'hour_end', 'fk_motorbike', 'fk_former', 'fk_student'];
protected $dates = ['date_seance'];
public function motorbikes(){
return $this->belongsTo('App\Motorbike', 'fk_motorbike');
}
public function formers(){
return $this->belongsTo('App\Former', 'fk_former');
}
public function students(){
return $this->belongsTo('App\Student', 'fk_student');
}
public function payments(){
return $this->hasMany('App\Payment', 'fk_training');
}
}
型号付款
class Payment extends Model
{
protected $fillable = ['date_payment', 'fk_student', 'method_payment', 'fk_type_seance', 'number_seance', 'total', 'fk_training'];
protected $dates = ['date_payment'];
public function students(){
return $this->belongsTo('App\Student', 'fk_student');
}
public function trainings(){
return $this->belongsTo('App\Training', 'fk_training', 'id');
}
我的关系很好
我必须如何限制训练?因为现在是无限的……付款时不考虑降神会的次数。
这是我的控制器培训中的函数 store() 编辑
public function store(Request $request)
{
$request->validate([
'date_seance' => 'required',
'hour_start' => 'required',
'hour_end' => 'required',
'fk_motorbike' => 'required',
'fk_former' => 'required',
'fk_student' => 'required'
]);
$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');
$fk_student = $request->get('fk_student');
$PaymentHasBeenMadeForThisTraining = Payment::where('fk_student', $fk_student)
->where('fk_training', $request->get('fk_training'))
->first();
$trainings = Training::with('payments')->get();
$johnsTrainings = Training::with(['payments' => function($query) use($fk_student){
$query->where('fk_student', $fk_student);
}])->get();
/*
if(isset($trainings)){
return redirect()->route('trainings.index')
->with('error', 'Duplicate ! ');
}*/
if(isset($johnsTrainings)){
return redirect()->route('trainings.index')
->with('error', 'Limit ! ');
}
else{
Training::create($request->all());
return redirect()->route('trainings.index')
->with('success', 'Add');
}
}
在测试你的代码时,我得到了这个:
【问题讨论】:
-
什么是
seance? -
@levi: 你好,培训