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

Hello, from the conversation on IRC, I concluded this could work:

$posts = Topic::where('slug', '=', $topic_slug)->posts()->take(5)->get();

assuming function posts() has many Post

Last updated 2 years ago.
0

Thanks delmadord,

That produced an method not found exception. Adding ->first() made it work. Final query:

$posts = Topic::where('slug', '=', $topic_slug)->first()->posts()->take(5)->get();

Fixed.

Last updated 2 years ago.
0

Nice to hear that.

Note that if there are no Topics matching given slug, FatalErrorException Call to a member function posts() on a non-object happens, you can thus modify your query to

$posts = Topic::where('slug', '=', $topic_slug)->firstOrFail()->posts()->take(5)->get();

Have a nice day.

Last updated 2 years ago.
0

That's right :)

Awesome stuff - thanks again.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

banago banago Joined 21 Jan 2014

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.