【发布时间】:2020-06-02 12:28:59
【问题描述】:
我有User 和Roles relationships 的模型,就像这样:
public function users(){
return $this->belongsToMany(User::class)->withTimestamps();
}
和角色模型与用户关系:
public function roles(){
return $this->belongsToMany(Role::class)->withTimestamps();
}
当然还有数据透视表"role_user" 包含id, user_id, role_id and timestamps
我尝试让用户 ordered By roles name 像这样:
$users = App\Models\User::with('roles')->orderBy('roles.name', 'desc')->get();
但我有这个错误:
Illuminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'roles.name' in 'order clause' (SQL: select * from `users` where `users`.`deleted_at` is null order by `roles`.`name` desc)
http://localhost:8000/users
请帮忙。
【问题讨论】:
标签: laravel eloquent-relationship