Support the ongoing development of Laravel.io →
Database Eloquent Queues
Last updated 2 years ago.
0

Why not just use the relation? Each Comment belongsTo a User, so all you need to do is create a relation in Comment model and then you can do something like this

$comments = Comment:with('user')->get();
...

you can than access user name for a particular one $comment

$comment->user->username
Last updated 9 years ago.
0

Hey Torch!

well this is how i did it, but i want to access it on client side via - i mean, i want it to be converted to Json with not only the user id (which is part of the comment, but also extra attributes which are only inside the user model

0

Sorry avieln, I dont understand what you are trying to achieve :/ Do you really need it in json? You can access the user model attributes of comment by the code i posted above. Maybe elaborate a little more where is the problem.

Not to mention, that the collection in laravel is returned as json automatically (I think). If not there is always toJson() method

$comments = Comment:with('user')->get()->toJson();
Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

avieln avieln Joined 29 Jan 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.