Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a column in laravel query result

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?

like image 521
Jaskaran Singh Puri Avatar asked Dec 29 '16 13:12

Jaskaran Singh Puri


People also ask

How do I select a specific column in laravel?

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.

How do you pluck in laravel?

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.

What is query Builder in laravel?

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.

How to retrieve a single row from a table in Laravel?

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:

How to check if record exists in collection in Laravel?

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.

How to use subqueries in orderby() method in Laravel?

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.

What is Laravel database?

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.


2 Answers

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')
like image 155
Alexey Mezenin Avatar answered Oct 22 '22 22:10

Alexey Mezenin


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);
}
like image 2
Amit Gupta Avatar answered Oct 22 '22 21:10

Amit Gupta