vendredi 29 décembre 2017

How can I insert two models at once with one model with multiple rows insert uisng Laravel 5.4

I have two migration tables Patrol(A) and Patrol_Details(B), I would like to insert to Patrol and same time to Patrol_details,Patrol, A hasMany B and B belongsTo A, in B would like to add multiple rows at once and also insert A_Id Here is My Controller, When I Try to insert, Insert only Table A

public function store(Request $request)
{
$this->validate($request, [
'unit_leader' => 'required',
'leader_no' => 'required',
'patrol_identity' => 'required',
'date' => 'required',       
]);
$patrols = new Patrol;
$patrols->unit_leader = $request->unit_leader;
$patrols->leader_no = $request->leader_no;
$patrols->patrol_identity = $request->patrol_identity;
$patrols->date = Carbon::parse($patrols->date)->format('Y/m/d');
$patrols->intels_id = $request->intels_id;
$patrols->save();


}
$rows = $request->input('rows');
foreach ($rows as $row)
{
$details[] = [
'full_name' => $row['full_name'],
'organisation' => $row['organisation'],
'position_grade' => $row['position_grade'],
            ];
}

 $patrols->patrol->Patrol_Detail()->save($patrol->Patrol_Detail);

$patrols->Patrol_Detail()->save($rows);
return redirect()->route('patrol.index')->with('success' , ' created successfully');

}

A MODEL

class Patrol extends Model
{
    public function intel()
    {
        return $this->belongsTo('App\Model\im\Intel');
    }

    public function patrol_detail()
    {
        return $this->hasMany('App\Model\im\Patrol_Detail','patrol_id','id');
    }
}

B MODEL

class Patrol_Detail extends Model
{

    protected $fillable = ['full_name', 'organisation','position_grade'];

    public function patrol()
    {
        return $this->belongsTo('App\Model\im\patrol');
    }
}

When I Try to insert, Insert only Table A, any guidance will be appreciated Thank you



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ClICkJ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire