model 的 with 怎么写 join 的条件
用户模型:
class User extends Model
{
/**
-
用户特长
*/
public function userSpec(){
return $this->hasMany(UserSpecialty::class, 'uid', 'id');
}
}
特长模型
class UserSpecialty extends Model
{
public function belongsToUser()
{
return $this->belongsTo(User::class, 'uid', 'id');}
public function cate()
{
return $this->has(Category::class, 'specialtyid', 'id');
}
}
现在我要找一个特长里面包括 php字符的用户
$oModel = new User();
$aList = $oModel->with("userSpec")->where($aFindArr)->limit(10)->get();
这里的where要怎么写?
如果UserSpecialty再有一个表和他一对一,比如
class SpecialtyCategory extends Model
{
public function belongsToUserSpecialty()
{
return $this->belongsTo(UserSpecialty::class, 'specialtyid', 'id');
}
}
这里
$aList = $oModel->whereHas("userSpec",
function($query) use ($search){
//TODO 这里应该怎么写
$query->where(['cate' => function ($catQuery) use ($search){
$catQuery->where('name','like','%',$search . '%')
->where('en_name','like', '%' . $search . '%');
}]);
}
)->limit(10)->get();
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: