Skip to content
Advertisement

How to display pagination links in a blade on Laravel 8?

I need to add pagination, but it seems complicated in my case, because I get data from database with paginate, but then I modify this data and when I call links() method on the blade, I get the following exception
Method IlluminateSupportCollection::links does not exist.
My code in the Controller method:

$transactionsByLastMonth = Transaction::where('created_at', '>=', Carbon::now()->subDays(30)->toDateTimeString())
    ->where('user_id', Auth::user()->id)
    ->with(['user', 'propertyFrom', 'propertyTo', 'paddockFrom', 'paddockTo', 'cattleTypeFrom', 'cattleTypeTo'])
    ->paginate(10);
        
$transactionsByDays = collect(TransactionResource::collection($transactionsByLastMonth))
    ->sortByDesc('created_at')
    ->groupBy(function($date) {
        return Carbon::parse($date['created_at'])->format('d');
    });

return view('user.reports.index', compact('transactionsByDays'));

Yes, a pagination limits my data to 10 rows, but due to I’m grouping data by days, my collection modifies itself and I can’t use $transactionsByDays->links() method to show pagination.
If see dd($transactionsByDays) , it looks like:

IlluminateSupportCollection {#1381 ▼
  #items: array:3 [▼
    "01" => IlluminateSupportCollection {#1019 ▼
      #items: array:7 [▼
        0 => array:16 [▶]
        1 => array:16 [▶]
        2 => array:16 [▶]
        3 => array:16 [▶]
        4 => array:16 [▶]
        5 => array:16 [▶]
        6 => array:16 [▶]
      ]
    }
    31 => IlluminateSupportCollection {#1386 ▼
      #items: array:2 [▼
        0 => array:16 [▶]
        1 => array:16 [▶]
      ]
    }
    30 => IlluminateSupportCollection {#1384 ▼
      #items: array:1 [▼
        0 => array:16 [▶]
      ]
    }
  ]
}

Therefore I need to paginate all arrays together which inside “01”, 31, 30… How can I do it?
Maybe rewrite code above in the controller somehow?
The main aim is that I need to group data to every day according to my json-resource class.
Any ideas?

Advertisement

Answer

I’ve found a solution gyus. I had to pass also a variable transactionsByLastMonth and use links() method over this.
In the meantime I use foreach on the my blade file over transactionsByDays variable. It’s correctly works together cause it uses the same data from database, just in the first case its not filtered and not grouped and in the second one it is.

return view('user.reports.index', compact('transactionsByLastMonth', 'transactionsByDays'));
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement