使用者模型:
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();