Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FatemeFa's avatar

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();
	}
}
0 likes
8 replies
Snapey's avatar

please format your code with three backticks ``` on a line before code and on its own line after code

1 like
Snapey's avatar

You just keep overwriting $date

Are you supposed to be doing something with it?

1 like
webrobert's avatar
Level 51

@fatemefa

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
		...
}

1 like
Snapey's avatar

@webrobert pagination implements arrayable so no special treatment is needed to iterate over the collection

webrobert's avatar

@Snapey ahh so it does. I didn't realize that that would work. Also just learned the actual issue here is that DB::table('itemdetails') doesn't provide a model instances, alas no carbon.

Please or to participate in this conversation.