Laravel 5.4 Eloquent Saving with relations with many

时间:2018-01-07 07:39:12

标签: php laravel-5 eloquent

我有以下模型,有hasMany和belongsTo关系,我想保存很多巡逻细节,Patrol,插入成功,但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');

    }

这是我的观点

    <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>

当我保存巡逻时,保存成功,但巡逻细节不保存,任何想法我出错了

1 个答案:

答案 0 :(得分:0)

很抱歉,但我没有看到&#34; $ pd-&gt; save()&#34;在你的代码中