Home > front end >  Laravel paginate() not returning results of nested relationship if limit is applied
Laravel paginate() not returning results of nested relationship if limit is applied

Time:10-23

While using nested relationships & applying a limit, the results returned by paginate(10) function are incorrect.

$products = Products::with(['customers' => function ($query) {
    $query->active();
}])
    ->paginate(10);

This piece of code returns the following data:

Array
(
    [current_page] => 1
    [data] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [description] => Sample Product Name
                    [created_at] => 2022-10-04T17:05:25.000000Z
                    [updated_at] => 2022-10-04T17:05:25.000000Z
                    [customers] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 15
                                    [product_id] => 1
                                    [customer_id] => 84
                                    [active] => 1
                                    [created_at] => 2022-10-04T17:05:25.000000Z
                                    [updated_at] => 2022-10-04T17:05:25.000000Z
                                )

                            [1] => Array
                                (
                                    [id] => 16
                                    [product_id] => 1
                                    [customer_id] => 86
                                    [active] => 1
                                    [created_at] => 2022-10-04T17:05:25.000000Z
                                    [updated_at] => 2022-10-04T17:05:25.000000Z
                                )

                        )

                )

            [1] => Array
                (
                    [id] => 2
                    [description] => Sample Product Name
                    [created_at] => 2022-10-04T17:05:25.000000Z
                    [updated_at] => 2022-10-04T17:05:25.000000Z
                    [customers] => Array
                        (
                        )

                )
        )
)

Which is correct. However, if I apply a limit to the results of customers, like such:

$products = Products::with(['customers' => function ($query) {
    $query->active()->limit(5);
}])
    ->paginate(10);

Suddenly the results are gone.

Array
(
    [current_page] => 1
    [data] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [description] => Sample Product Name
                    [created_at] => 2022-10-04T17:05:25.000000Z
                    [updated_at] => 2022-10-04T17:05:25.000000Z
                    [customers] => Array
                        (
                        )
                )

            [1] => Array
                (
                    [id] => 2
                    [description] => Sample Product Name
                    [created_at] => 2022-10-04T17:05:25.000000Z
                    [updated_at] => 2022-10-04T17:05:25.000000Z
                    [customers] => Array
                        (
                        )

                )
        )
)

Why does this happen and how can I solve it?

CodePudding user response:

From Laravel docs:

The limit and take query builder methods may not be used when constraining eager loads.

https://laravel.com/docs/9.x/eloquent-relationships#constraining-eager-loads

  • Related