mercredi 7 mars 2018

Laravel updating related models and tables

For example, I have this block of code.

public function update(Request $request, $id){
    $abc = Model::find($id);
    $abc->column1 = $request->input1;
    $abc->column2 = $request->input2;
    $abc->save();
}

On save of the $abc object, I have to update a related model. That goes like this.

Note: $abc hasMany $def;

public function update(Request $request, $id){
    $abc = Model::find($id);
    $abc->column1 = $request->input1;
    $abc->column2 = $request->input2;
    $abc->save();

    if($abc){
        foreach($request->data){
             //what should I do here to update the rows in the table that 
               has the same $abc->id as above ?
        }
    }
}

I have seen associate(), $abc->def()->save($abc), but can't think of a way on how to properly save the array of data to the related table.



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

Aucun commentaire:

Enregistrer un commentaire