Laravel Eloquent Relations: ->latest()

前端 未结 3 336
轻奢々
轻奢々 2020-12-15 16:23

What is the function of latest() in laravel?

Example:

public function activity()
{
    return $this->hasMany(\'App\\Activity\')
        ->with(         


        
相关标签:
3条回答
  • 2020-12-15 17:01

    latest() is a function defined in Illuminate\Database\Query\Builder Class. It's job is very simple. This is how it is defined.

    public function latest($column = 'created_at')
    {
        return $this->orderBy($column, 'desc');
    } 
    

    So, It will just orderBy with the column you provide in descending order with the default column will be created_at.

    0 讨论(0)
  • 2020-12-15 17:06

    ->latest() fetches the most recent set of data from the Database. In short, it sorts the data fetched, using the 'created_at' column to chronologically order the data.

    0 讨论(0)
  • 2020-12-15 17:07

    add ->get() on your code like that

     public function activity()
            {
                return $this->hasMany('App\Activity')
                    ->with(['user', 'subject'])
                    ->latest()->get();
            }
    
    0 讨论(0)
提交回复
热议问题