簡體   English   中英

Laravel組合了where子句

[英]Laravel combined where clause

我想獲取所有具有一個或另一個角色的用戶。 我已經嘗試過這種方法,但它不起作用:

$dentist = Role::where('name', 'dentist')->orWhere('name', 'local_admin')->first()->users()->where('clinic_id', $user->clinic_id)->get();

$dentist = Role::where('name', ['dentist', 'local_admin')->first()->users()->where('clinic_id', $user->clinic_id)->get();

$dentist = Role::whereIn('name', ['dentist', 'local_admin')->first()->users()->where('clinic_id', $user->clinic_id)->get();

有一個簡單的解決方案嗎?

你必須定義關系 Role的hasMany users ,那么你可以訪問userswith

$dentist = Role::with(['users' => function($query){
    $query->where('clinic_id', $user->clinic_id);
  })])
  ->where('name', 'dentist')
  ->orWhere('name', 'local_admin')
  ->first();

這將成為您的榜樣

/**
 * Get the users for the role.
 */
public function users()
{
    return $this->hasMany(User::class);
}

這將在您的用戶模型中

/**
 * Get the role the user.
 */
public function role()
{
    return $this->belongsTo(Role::class);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM