vals's avatar
Level 1

Strange ->paginate(5) of relation // 1st Page = results 2nd Page no Results but SQL gets Results

Hello,

i have a strange behavior with a pagination. The first call of the method gets me results. If i increment the page i dont get any results.

Short the Structure: Group -> Kategorie -> Item

If i run the build SQL's on the Database i get results.

Build SQL first Page:

select `itemidees`.*, `itemidee_groups_to_itemidees`.`itemidee_kategorie_id` as `pivot_itemidee_kategorie_id`, `itemidee_groups_to_itemidees`.`itemidee_id` as `pivot_itemidee_id` from `itemidees` inner join `itemidee_groups_to_itemidees` on `itemidees`.`id` = `itemidee_groups_to_itemidees`.`itemidee_id` where `itemidee_groups_to_itemidees`.`itemidee_kategorie_id` in ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10') limit 5 offset 0
mysql> select `itemidees`.id, `itemidee_groups_to_itemidees`.`itemidee_kategorie_id` as `pivot_itemidee_kategorie_id`, `itemidee_groups_to_itemidees`.`pivot_itemidee_id` from `itemidees` inner join `itemidee_groups_to_itemidees` on `itemidees`.`id` = `itemidee_groups_to_itemidees`.`itemidee_id` where `g
ups_to_itemidees`.`itemidee_kategorie_id` in ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10') limit 5 offset 0;
+----+---------------------------------+-----------------------+
| id | pivot_itemidee_kategorie_id | pivot_itemidee_id |
+----+---------------------------------+-----------------------+
|  1 |                               1 |                     1 |
|  2 |                               1 |                     2 |
|  3 |                               1 |                     3 |
|  4 |                               1 |                     4 |
|  5 |                               1 |                     5 |
+----+---------------------------------+-----------------------+
5 rows in set (0.00 sec)

Now the build SQL with Page 2:

select `itemidees`.*, `itemidee_groups_to_itemidees`.`itemidee_kategorie_id` as `pivot_itemidee_kategorie_id`, `itemidee_groups_to_itemidees`.`itemidee_id` as `pivot_itemidee_id` from `itemidees` inner join `itemidee_groups_to_itemidees` on `itemidees`.`id` = `itemidee_groups_to_itemidees`.`itemidee_id` where `itemidee_groups_to_itemidees`.`itemidee_kategorie_id` in ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10') limit 5 offset 5
mysql> select `itemidees`.id, `itemidee_groups_to_itemidees`.`itemidee_kategorie_id` as `pivot_itemidee_kategorie_id`, `itemidee_groups_to_itemidees`.`pivot_itemidee_id` from `itemidees` inner join `itemidee_groups_to_itemidees` on `itemidees`.`id` = `itemidee_groups_to_itemidees`.`itemidee_id` where `g
ups_to_itemidees`.`itemidee_kategorie_id` in ('1', '2', '3', '4', '5', '6', '7', '8', '9', '10') limit 5 offset 5;
+----+---------------------------------+-----------------------+
| id | pivot_itemidee_kategorie_id | pivot_itemidee_id |
+----+---------------------------------+-----------------------+
|  6 |                               2 |                     6 |
|  7 |                               2 |                     7 |
|  8 |                               3 |                     8 |
|  9 |                               3 |                     9 |
| 10 |                               3 |                    10 |
+----+---------------------------------+-----------------------+
5 rows in set (0.00 sec)

If i dd($itemcats) and navigate to the relation, i get an empty collection.

Here I have the Code:

 public function ajax_filterByCats($catsIdArr = [1,2,3,4,5,6,7,8,9,10], $output = 'html')
    {

        $retcollection = \Illuminate\Database\Eloquent\Collection::make();
        $randomretcollection = \Illuminate\Database\Eloquent\Collection::make();


        $itemcats = \App\ItemideeKategorie::whereIn('id' ,$catsIdArr)
            ->with(['itemideen' => function($query) {
                // return $query->paginate(5);
                $page = (int) $_REQUEST['page'];
                return $query->limit(5)->offset(5*$page);
            }])->get();

        foreach ($itemcats as $itemcat) {
            foreach ($itemcat->itemideen as $item)
            {
                $retcollection->push($item);
            }
        }

        if($output == 'html')
            return view('itemideen._itemidee_list')->with(['itemideen' => $randomretcollection]);

        return $retcollection;

        /*
        foreach ($retcollection as $rc)
        {
            $randomretcollection->push($retcollection->random());
        }
        */
    }
0 likes
6 replies
thefuzzy0ne's avatar

I think the call to paginate() needs to go OUTSIDE of the closure, otherwise you're only paginating that inner query.

vals's avatar
Level 1

I need all categories but only the first 5 items each, paginated. If i set the paginate outside of the closure i only get 5 categorys with all items in it.

The strange thing is, the database serves the data, but it seems not to be mapped into :-(

vals's avatar
Level 1

Sure, also can use a raw SQL. Just thought i use the easy way with elequent relations. Although the data is already queried right from the database and just not accessable throught the relation. Bug?

vals's avatar
Level 1

Just tryed the join: That way I also just get x Elements from one category, not x each category.

$gicat = \App\ItemideeKategorie::whereIn('itemidee_kategories.id' ,$catsIdArr)
            ->join('itemidee_groups_to_itemidees', 'itemidee_groups_to_itemidees.itemidee_kategorie_id', '=', 'itemidee_kategories.id')
            ->join('itemidees', 'itemidee_groups_to_itemidees.itemidee_id', '=', 'itemidees.id')
            ->select('itemidees.*')
            ->limit(5)
            ->get();

My ugly solution would now be a foreach of the categorys and adding the results to a new collection.

thefuzzy0ne's avatar

I'm a little confused. It would help a lot if I could see your models from each side of the pivot table. So long as the relations are set up correctly, you should be able to grab everything with a simple join.

Please or to participate in this conversation.