How to use eloquent ->with method on collection

  laravel, laravel-5, laravel-collection

You may find the question stupid. I may not be doing the research well so I thought to ask it here. I would like to know how to use ->with on collection or what is its equivalent. Here is the code:

$theLastCommentOfThisPost = $post->comments->with('user')->sortByDesc('id')->first();

I would like to get something like this:

IlluminateSupportCollection Object
(
    [items:protected] => Array
        (
            [id] => 19
            [commentable_type] => AppPost
            [commentable_id] => 123
            [comment] => totoototo
            [is_approved] => 1
            [user_id] => 1
            [created_at] => 2019-04-01
            [updated_at] => 2020-02-04
            [user] => AppUser Object
                    (
                        [id] => 1
                        [name] => AppPost
                        [email] => [email protected]
                    )
        )

)

thanks

Source: Laravel

Leave a Reply