[英]Laravel 5.4 Eloquent Saving with relations hasMany
I have the following Models, with hasMany and belongsTo relationship, i Would like to save many details of patrol, Patrol,insert successfully but the PatrolDetails does not insert any details in the database, PATROL 我有以下具有hasMany和belongsTo关系的模型,我想成功保存巡逻,巡逻,插入的许多详细信息,但PatrolDetails不会在数据库PATROL中插入任何详细信息
<?php
class Patrol extends Model
{
public function patrol_detail()
{
return $this->hasMany('App\Patrol_Detail','patrol_id','id');
}
}
PATROL DETAILS 巡逻细节
<?php
class PatrolDetail extends Model
{
protected $fillable = ['full_name', 'organisation','position_grade'];
public function patrol()
{
return $this->belongsTo('App\Patrol');
}
}
HERE MY CONTROLLER 在这里我的控制器
public function store(Request $request)
{
// dd($request);
$this->validate($request, [
(validate)
]);
$patrols = new Patrol();
(patrols request)
$patrols->save();
for ($i=0; $i<count($request->full_name); $i++){
$pd = new PatrolDetail;
$pd->where('patrols_id',$patrols->id)->delete();
if(empty($request->full_name[$i]) || empty($request->organisation[$i]) || empty($request->position_grade[$i])){
session()->flash('error','All participant entries must be specified');
$patrols->delete();
return redirect()->back()->withInput();
$pd->full_name = $request->full_name;
$pd->organisation = $request->rganisation;
$pd->position_grade = $request->position_grade;
}
}
// redirect somewhere after
if($patrols){
return redirect()->route('patrol.index')
->with('successs' , 'created successfully');
}
return back()->withInput()->with('errors', 'Error creating Details');
}
HERE IS MY VIEW 这是我的看法
<td class="no">1</td>
<td>
<input type="text" class="full_name form-control input-normal" name="full_name[]" value="">
</td>
<td><input type="text" class="organisation form-control input-normal" name="organisation[]" value="">
</td>
<td><input type="text" class="position_grade form-control input-normal" name="position_grade[]" value="">
</td>
<td>
When I save an Patrol, saves successfully, but the Patrol details dont save, any idea where i am going wrong 当我保存巡逻时,保存成功,但是巡逻详细信息不保存,任何想法我都会出错
抱歉,我在您的代码中看不到“ $ pd-> save()”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.