please format your code with three backticks ``` on a line before code and on its own line after code
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$created_at
Hi! I want to use Carbon and I need to have created_at but in my query builder with pagination there is a problem that I can't solve it and shows this error: Undefined property: Illuminate\Pagination\LengthAwarePaginator::$created_at please help me!
public function userPanel(){
$userid = Auth::id(); //Query Builder
$items = DB::table('itemdetails')
->join('cities', 'itemdetails.city_id', '=', 'cities.id')
->join('universities', 'itemdetails.uni_id', '=', 'universities.id')
->select('itemdetails.name as adv_name',
'itemdetails.img as img',
'itemdetails.id as item_id',
'itemdetails.user_id as user_id',
'cities.name as city_name',
'universities.name as uni_name',
'itemdetails.created_at as created_at')
->where('itemdetails.user_id', '=', $userid)
->orderBy('itemdetails.created_at','desc')
->paginate(8);
foreach ($items->created_at as $item) {
Carbon::setLocale('fa');
$date = Carbon::parse($item->created_at)->diffForHumans();
}
}
you think the $items is a collection like when you use get() but it's not. In the Controller it's a paginator instance. dd it and see for yourself.
dd($items)
If you want to interact with the items use $items->items() as Item.
Items() is a method on paginator to access the items. But as written your code isn't going to change the values in the paginator. You are just renaming $date as @snapey said.
for less confusion on naming...
$paginate = DB::table('itemdetails')
...
->where('itemdetails.user_id', '=', $userid)
->orderBy('itemdetails.created_at','desc')
->paginate(8);
foreach ($paginate->items() as $item) {
// $item->created_at
...
}
Please or to participate in this conversation.