Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

firescript's avatar

Return an eloquent model with another model inside of it

Alright so I'm pretty confused right now...

Here is my events list

$events = CliqueEvent::where('user_id', '=', $id);
// i want to attach the friends of each event inside this $events variable

i can access an events friends like this but don't know how to when i have multiple events like the return set above.

$event = CliqueEvent::find(1)->friends;

What do I need to do to get the friends associated with each event in the $events object into the returned object. I want to display the friends of the event with only one call to the server.

Any help is appreciated.

0 likes
11 replies
firescript's avatar

Hey Valorin, I think my relationships are set up properly. Many to many for events and friends. I'm just struggling with this:

I have a query that is bringing back multiple events with only the event data. I want to also have in that return the friends associated with each event it returns.

So events would return something like this:

Name Address Friends [ bob, sally, tom ]

see what im saying? I just don't get how to get the friends model into that return set.

This is my event model relationships

    public function user()
    {
        return $this->belongsTo('User');
    }
    public function friends(){
        return $this->belongsToMany('Friend')->withTimestamps();
    }

This is my friends model relationships

    public function user()
    {
        return $this->belongsTo('User');
    }
    public function cliqueEvents(){
        return $this->belongsToMany('CliqueEvent')->withTimestamps();
    }

User model relationships

    public function friends()
    {
        return $this->hasMany('Friend');
    }

    public function cliqueEvents()
    {
        return $this->hasMany('CliqueEvent');
    }
Valorin's avatar

Ah, I think I know what you're saying - sorry, I miss-understood your original post :-)

So you want to return the associated Friends when you have a collection of Events, rather than looping through each Event and returning the Friends for each and merging?

I don't know if it's possible, since the relationships are defined on the model and not the collection class. You could extend the Collection class and do the loop-merge internally, so the external collection call looks like this:

$friends = $eventCollection->friends();

Not as nice a solution, but it might be your only option.

firescript's avatar

I want to merge the friends into the Events collection so it is accessible in my view.

Are you thinking something like this?

    $events = CliqueEvent::where('user_id', '=', $id)->get();

    foreach ($events as $event) {

        $eventFriends = $event->friends;
        // just not sure how to merge this with the events collection

    }
Valorin's avatar

What do you mean by accessible in your view? If you send $events in that last example to the view, then you'll be able to loop it and access the friends attribute in the view, which will contain the linked friends model collection automatically.

If the code you've just provided works in your collector, then the only way that I can see that it wouldn't work in the view is if you're returning an array of the collection, rather than the full collection model?

firescript's avatar

AHH sorry man shoulda clarified, I'm using angular.js and it is GET requesting a route which currently is performing this action:

      $user = User::find($id);    
      return $user->cliqueEvents;

I tried doing it purely laravel and it works perfectly. I can access the friends directly through it. But if I want to use Angular and receive a JSON object via the route I need to do some collection manipulation I guess eh?

Valorin's avatar

Ah gotcha. Something I just stumbled upon which might work, try eager loading the relationships:

$user = User::whereId($id)->with('user', 'friends')->get();    
return $user->cliqueEvents;

In theory it will load the user and friends relationship models, and when the JSON converter turns your collection into JSON, it should have those records in memory too - maybe...

Or if that doesn't work, try and extend the Model::toArray() function, see: https://github.com/laravel/framework/blob/4.2/src/Illuminate/Database/Eloquent/Model.php#L2212. Might be able to inject them manually via that.

2 likes
firescript's avatar

VALORIN!! I <3 YOU

$id = Auth::user()->id;
$events = CliqueEvent::where('user_id', '=', $id)->with('friends')->get();    
return $events;    

that did it my friend :)

Thanks so much for your help

1 like
Valorin's avatar

Awesome, I was hoping that would work :-)

It's a handy trick to remember, and will also cut down your SQL queries and speed up the entire request.

1 like
Pixelpanther's avatar

Thank you @bashy . I know this is 3+ years old, but I've been trying to figure out how to return an object with nested relations (for use in JSON), and the dot notation is exactly what I needed.

Please or to participate in this conversation.