I'm getting an error in my controller Undefined property: Illuminate\Database\Eloquent\Collection::$created_at
How can I access a column created_at
from a result of laravel query?
Controller Code:
public function getDashboard() {
$posts=Post::latest('created_at')->get();
dd($posts->created_at);
// $posts->created_at=Carbon::parse($posts->created_at)->diffForHumans(Carbon::now());
return view('dashboard',compact('posts'));
}
It works with findOrFail(some_id)
but not with this, why?
Select specific columns with Laravel Eloquent To get all of the columns from the users table, we would use the following: $user = User::where('username', 'bobbyiliev')->get(); However, if you wanted to get only a specific column, you could pass it as an argument to the get() method.
The Laravel pluck () as the name suggests, extracts certain values, as suggested. It literally plucks the information out from the given array and produces it as an output. However, it is most effective against objectives, but will work well against arrays too.
Introduction. Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works perfectly with all of Laravel's supported database systems.
For more information on Laravel collections, check out the collection documentation. Retrieving A Single Row / Column From A Table If you just need to retrieve a single row from a database table, you may use the DBfacade's firstmethod. This method will return a single stdClassobject:
To check if record exists in collection in laravel, laravel provides exists () eloquent method. Using this exists () method, we can easily check if collection exists laravel. If you don't know laravel check if collection is empty, then this tutorial is for you.
The second way is using a subquery. As of Laravel 6, the orderBy () and orderByDesc () query builder methods support passing a query, instead of just a column name. When you do this, the query is executed as a subquery within the order by statement.
Database: Query Builder - Laravel - The PHP Framework For Web Artisans Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
get()
returns collection, so you need to iterate over it:
foreach ($posts as $post) {
echo $post->created_at;
}
Or you could use first()
to get object instead of collection. In this case this would work:
$post = Post::latest()->first();
$post->created_at;
Also, you don't need to pass created_at
to latest()
, because this column is defined as default:
public function latest($column = 'created_at')
As $post
is an instance Collection
you have to use foreach
as:
foreach ($posts as $post) {
dd($post->created_at);
}
Or you can use first
to get first object or last
to get last object as
dd($posts->first()->created_at);
dd($posts->last()->created_at);
Update
Try it as:
foreach ($posts as $post) {
$post->diff_for_humans = $post->created_at->diffForHumans();
}
Then your can access it as:
foreach ($posts as $post) {
dd($post->diff_for_humans);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With