我的恋爱关系接近5级。
在刀片中,我一直都在使用,没有问题。例如:
{{ $user->team->department->region->company->name ?? ''}}
我将如何查询属于公司的所有用户?
$users = User::where($user->team->department->region->company->id, '=', 1)->get(); ?????
公司关系:
class Company extends Model
{
public function region() {
return $this->hasMany('App\Region', 'company_id', 'id');
}
}
区域关系:
class Region extends Model
{
public function company() {
return $this->hasOne('App\Company', 'id', 'company_id');
}
public function department() {
return $this->hasMany('App\Department', 'region_id', 'id');
}
}
部门关系:
class Department extends Model
{
public function region() {
return $this->hasOne('App\Region', 'id', 'region_id');
}
public function team() {
return $this->hasMany('App\Team', 'department_id', 'id');
}
}
团队关系:
class Team extends Model
{
public function department() {
return $this->hasOne('App\Department', 'id', 'department_id');
}
public function user() {
return $this->hasMany('App\User', 'team_id', 'id');
}
}
用户关系:
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function team() {
return $this->hasOne('App\Team', 'id', 'team_id');
}
}
我知道必须有一种简单的方法来查询Laravel提供的方法或某种便利的嵌套关系,但是我还没有从Googling那里找到任何不错的例子。
答案 0 :(得分:0)