V
V
Viktor K2019-09-07 17:34:35
Laravel
Viktor K, 2019-09-07 17:34:35

How to return an object with relations as objects?

I give out a list of objects via api in the form of json.
An object has relationships with other classes through belongTo and so on.
By default, when using this method, I get the id of related objects in json. Of course, before returning, I can bypass the array of returned objects and push each entity instead of id, but these are some kind of crutches. Maybe there is a regular way that I can't find in the docks?
return response()->json($events, 200);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
JhaoDa, 2019-09-07
@vuldozer

Is there a regular way that I can't find in the docks?
https://laravel.com/docs/6.0/eloquent-resources

A
Alexander Filippenko, 2019-09-07
@alexfilus

If possible, it is best to add to the with request.
If not, then you can call entities by relationships, then the data will appear in the $events object

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question