简体   繁体   中英

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 Illuminate\\Support\\Collection::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:

Illuminate\Support\Collection {#1381 ▼
  #items: array:3 [▼
    "01" => Illuminate\Support\Collection {#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 => Illuminate\Support\Collection {#1386 ▼
      #items: array:2 [▼
        0 => array:16 [▶]
        1 => array:16 [▶]
      ]
    }
    30 => Illuminate\Support\Collection {#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?

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'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM