D
D
dk-web2019-03-18 13:30:24
Laravel
dk-web, 2019-03-18 13:30:24

How to properly update related models using Resource in Laravel?

There are 2 models - User and Country. Country_id is stored in the User model, they are linked by this key.
Made UserResource

return [
            'id' => $this->id,
            'name' => $this->name,
            'country' => $this->country->eng_name
        ];

And how to update without id? Or Resource for display only.
Something seems to have missed the logic.
Previously, I made a selection through with, connected models and laid out further. For a presentation, this is convenient, but for Crud? I would appreciate examples.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2019-03-18
@jazzus

Update better in controllers.
For example. Country_id is passed from the view to the controller along with other data. Then in the controller, in the store method
or You can give the relationship object through the resource like this

return [
            // Отношения
            'country' => new CountryResource($this->country)
        ];

before that, you need to create a resource for the country and in the controller to chain objects through with , each user in the collection will have a country object

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question