xxxxxxxxxx
<?php
public function comments()
{
return $this->hasMany('Comment')->orderBy('column');
}
// two
class User
{
public function comments()
{
return $this->hasMany('Comment');
}
}
class Controller
{
public function index()
{
$column = Input::get('orderBy', 'defaultColumn');
$comments = User::find(1)->comments()->orderBy($column)->get();
// use $comments in the template
}
}
xxxxxxxxxx
public function latestPost()
{
return $this->hasOne(\App\Post::class)->latest();
}
$users = Topic::with('latestPost')->get()->sortByDesc('latestPost.created_at');
xxxxxxxxxx
$users = User::with(['student' => function ($q) {
$q->orderBy('id', 'desc');
}]);
xxxxxxxxxx
$order = 'desc';
$users = User::join('roles', 'users.role_id', '=', 'roles.id')->orderBy('roles.label', $order)->select('users.*')->paginate(10);
xxxxxxxxxx
laravel query order by relationphp by Alberto Peripolli on Oct 26 2020 Donate Comment