Support the ongoing development of →
Database Eloquent Queues
Last updated 2 weeks ago.

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

Last updated 7 years ago.

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


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 7 years ago.

Sign in to participate in this thread!


Your banner here too?

avieln avieln avieln Joined 29 Jan 2015


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

Your logo here?

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

© 2022 - All rights reserved.