Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

Hey on my cell, but something like this:

$blog = Blog::where ('id','=', $id)->with (['blogcomments.user])-> first ();

If you want all blog post remove the where() and replace first() with get()

Last updated 1 year ago.
0

makzumi said:

Hey on my cell, but something like this:

$blog = Blog::where ('id','=', $id)->with (['blogcomments.user])-> first ();

If you want all blog post remove the where() and replace first() with get()

That's not helping me man .. i just got an error

BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::user()
Last updated 1 year ago.
0

makzumi said:

I think you need to read this:

http://laravel.com/docs/eloquent#relationships

and

http://laravel.com/docs/eloquent#eager-loading

Thanks a lot for your help. I did manage to get 3 tier data by using

$blog = Blog::with('user')->with('blogcomments')->where('blogs.title', '=', "$id")->get();

This is the data i got:

[{"id":1,"title":"this is a test title","body":"and this is a test body","user_id":1,"created_at":"2014-06-08 05:30:10","updated_at":"2014-06-08 08:56:14","user":{"id":1,"firstname":"Salil","lastname":"Bhattarai","email":"[email protected]","created_at":"2014-06-08 05:27:34","updated_at":"2014-06-08 05:32:16","remember_token":"cbFuUTolnpnTqz2W40VcEs6ZryUjlkU1QStyxOb3AZX5AY5QYnrHL7sKGtd6","picture":null,"active":1,"activation_code":"4w3jizc"},"blogcomments":[{"id":1,"user_id":2,"blog_id":1,"body":"test comment","created_at":"2014-06-08 07:34:13","updated_at":"2014-06-08 07:34:13"},{"id":2,"user_id":1,"blog_id":1,"body":"asldas ldjkaks","created_at":"2014-06-08 08:36:32","updated_at":"2014-06-08 08:36:32"}]}]

However, I'm still not getting the names of users of the blogcomments portion.

Is there anything that i'm missing?

Last updated 1 year ago.
0

Do it like this:

$blog = Blog::with('user.blogcomments')->where('blogs.title', '=', "$id")->get();

only one "with". If your relations are set properly you should see the users.

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

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.