我有一个公司和药房的模型
<?php
// Company.php
public function dispensary()
{
return $this->hasMany(Dispensary::class);
}
// Dispensary.php
public function company()
{
return $this->belongsTo(Company::class);
}
我想要的是首先获得用户所属公司的药房。
$auth = Auth::user();
$userCompany = $auth->company()->get();
$dispensaries = Company::find($userCompany->first()->id)->dispensary;
我应该如何构建一个查询,用户可以在其中搜索用户所属的药房列表。
我试过使用wherehas,但它是从公司而不是药房搜索
的Company::find($userCompany->first()->id)
->whereHas('dispensary', function ($query) use ($name) {
$query->where('name', 'like', "%{$name}%");
})->get();
好的,我终于通过使用 where 子句使其工作
$company = Company::find($id)->dispensary()
->where('name', 'like', "%{$name}%")
->where('contact_email', 'like', "%{$email}%")
->get();