How to update multiple models in one fucntion inside controller - Laravel vue
I am trying to update multiple models that related to each other with PK and FK.
I tried to update the first model "matter_dispute" and successfully updated so I repeated the same thing to update the other model "matter_dispute_c" but it didn't affect anything.
Here is the update function in controller:
public function update(Request $request, matter_dispute $matter_di开发者_开发问答spute, matter_dispute_b $matter_dispute_b, matter_dispute_c $matter_dispute_c)
{
$request->validate([
'pic_1'=>'required',
'pic_2'=>'required',
'remarks'=>'required',
'file_ref_no'=>'required',
]);
$postData = [
'pic_1' => $request->pic_1,
'pic_2' => $request->pic_2,
];
$postData2 = [
'remarks' => $request->remarks,
'file_ref_no' => $request->file_ref_no,
];
$matter_dispute->update($postData);
$matter_dispute_c->update($postData2);
}
I am not sure if this is the right approach to do it, is it better if i use query for it?
Because the way I am getting data as showing below:
$matter_dispute_all_tables = DB::table('matter_disputes')
->join('matter_dispute_bs', 'matter_disputes.id', '=', 'matter_dispute_bs.matter_disputes_id')
->join('matter_dispute_cs', 'matter_disputes.id', '=', 'matter_dispute_cs.matter_disputes_id')
->select('matter_disputes.*', 'matter_dispute_bs.*', 'matter_dispute_cs.*')
->get()
;
return response()->json($matter_dispute_all_tables);
So which approach is better and how can I achieve it?
I think you need to look into Laravel Relationships
And then also How to update models
精彩评论