Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vtxmg's avatar

How to limit records with with() method

User::with(['posts' => function($query) {
    $query->where('created_at', '>', $someDate);
}])->get();

It works fine.

Now I want to limit the no. of posts and i used

User::with(['posts' => function($query) {
    $query->where('created_at', '>', $someDate)->take(5);
}])->get();

but didn't return any value.

0 likes
10 replies
crazymonster's avatar

I would try:

User::with(['posts' => function($query) {
$query->where('created_at', '>', $someDate);
}])->take(5)->get();
mstnorris's avatar
User::with(['posts' => function($query) {
    $query->where('created_at', '>', $someDate);
}])->take(5);
1 like
bobbybouwmann's avatar
Level 88

You can either update the relation to the example below or use take on the query, so not in the callback but after the callback

// App\User.php

class User extends Model {

    public function posts()
    {
        return $this->hasMany('App\Post');
    }

    public function limitPosts()
    {
        return $this->hasMany('App\Post')->limit(5);
    }

}

Now you can do this

User::with(['limitPosts' => function($query) {
    $query->where('created_at', '>', $someDate);
}])->get();
2 likes
vtxmg's avatar

@bobbybouwmann I tried your method but didn't get desired result.. Instead it doesn't return any value or only some.

When I remove ->limt(10) then it return all records...

vtxmg's avatar

This is in Menu.php model

public function limitNewsTen(){
        return $this->hasMany('App\News','category_id');
    }

This is in my HomeController.php

$categories = Menu::whereStatus(0)->orderBy('page_category_order','asc')
                                        ->with(['limitNewsTen'=>function($query){
                                            $query->orderBy('id','desc');
                                        }])->get();

@bobbybouwmann Once again the question: I want all menu with 10 news on each.

bobbybouwmann's avatar

Mmh looks fine to me.. You are sure you get the correct data if you leave of limit()?

vtxmg's avatar

Yaa.. but If I do this:

public function limitNewsTen(){
       public function limitNewsTen(){
        return $this->hasMany('ProNews\News','category_id')->limit(10)->whereStatus_id(1)->whereStatus(0)->orderBy('id','desc');
    }

In Home controller :

$categories = Menu::whereStatus(0)->orderBy('page_category_order')->get();

And View:

@foreach($categories as $category)
    @foreach($category->limitNewsTen as $news)
        {!! $news->title !!}
    @endforeach
@endforeach

@bobbybouwmann Now it works fine but isn't there any way we can do where,orderBy stuffs on controller instead in model?

JarekTkaczyk's avatar

@vtxmg @bobbybouwmann Don't use limit/take in with and get - it's not what you expect, it won't work as expected ever.

Meaning - you can't limit related models per each parent model.

The only case you would do it is when you fetch single parent:

$model = Model::with(['someRelation' => function ($q) {
    $q->take(10);
}])->find($someId);

Read this for more http://softonsofa.com/tweaking-eloquent-relations-how-to-get-n-related-models-per-parent/

2 likes
fxcjahid's avatar
		->get()
		->map(function ($category) {
			$category->post = $category->post->take(3);
			return $category;
		});

Please or to participate in this conversation.