简体   繁体   中英

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

    <?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()”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM