lundi 11 juin 2018

Eloquent - edit table rows and return new result data - old data retrieved

I have a form where the user can edit, create or delete shipping methods. The user sends the form and the data is updated.

I want to return the user's shipping methods after they are edited. But I seem to get the old data back, instead of the updated data.

$user = \App\User::where('user_id', $user->id)->first();

            $user->shipping_methods->each(function($method) {
                $method->delete();
            });
            $methods = [];
            foreach ($request->input('methods') as $method) {
                $methods[] = new \App\ShippingMethod($method);
            }
            $user->shipping_methods()->saveMany($methods);
        }

    return response()->json($user->shipping_methods->toArray());

(at the moment the code just deletes the old shipping methods and replaces them with the new ones). I am using eloquent relations to get the shipping methods.

So when I do:

return response()->json($user->shipping_methods->toArray());

how come I don't get the new results, instead I get the results from before the update? Is it using the results from the first $user->shipping_methods at line 3? Should I "refresh" the query somehow?



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

Aucun commentaire:

Enregistrer un commentaire