我正在使用 eloquent 查詢構建器根據用戶搜索查詢提供的引數有條件地構建搜索查詢。
這似乎適用于直接屬于我正在搜索的模型的屬性。但是,在嘗試回傳與模型相關但不直接屬于模型的其他結果時,我遇到了問題。
我正在嘗試做的正是:
我試圖允許用戶按教師姓名搜索學校,其中姓名/關鍵字通過中間表出現在學校的關系中。
這是我的模型和關系:
學校:
class School extends Authenticatable
{
protected $fillable = [
'school_name', 'head_teacher', 'address'
];
public function teachers()
{
return $this->belongsToMany(Teacher::class, 'teacher_links', 'school_id', 'teacher_id');
}
}
老師:
class Teacher extends Authenticatable
{
use Notifiable;
protected $table = 'teacher';
protected $fillable = [
'name'
];
}
老師鏈接:
class TeacherLink extends Authenticatable
{
protected $table = 'teacher_links';
protected $fillable = [
'teacher_id', 'school_id'
];
我可以使用控制器通過學校成功查詢教師,$school->teachers因此我知道這種關系運行良好。
這是我的控制器代碼,其中有條件地建立用戶搜索引數:
public function index(Request $request)
{
$schools = School::query();
// User Search Term
$search = $request->get('search');
$headTeacher = $request->get('head_teacher');
$questions
->when($search, function ($q) use ($search) {
$q->where('school_name', 'like', '%' . $search . '%')
->orWhere('head_teacher', 'like', '%' . $search . '%')
// This is the query i'm having an issue with
->whereHas('teachers', function($q) use ($search) {
$q->where('teacher_name', 'like', '%' . $search . '%');
});
})
}
The two initial where clauses return the correct results when searching by a school name or head_teacher as these two properties directly belong to a school. However, i'd like to return additional schools that include a teachers name through the relationship.
So a school can belong to many teachers and vice versa through the intermediary teacher_link table but i don't know how to accurately filter this this using the eloquent query builder.
Initially i tried using the union function and joins which worked but broke subsequent where clauses as it doesn't seem to allow where clauses to follow after union queries.
It would be great if someone could tell me what i'm doing wrong and how i can fix the query in question:
// This is the query i'm having an issue with
->whereHas('teachers', function($q) use ($search) {
$q->where('teacher_name', 'like', '%' . $search . '%');
});
EDIT
I have tried the following which seems to work but it is slow:
$schools
->when($search, function ($q) use ($search) {
$q->join("teacher_links", function ($join) {
$join->on("teacher_links.school_id", "=", "schools.id");
})
->join("teachers", function ($join) {
$join->on("teachers.id", "=", "teacher_links.teacher_id");
})
->select("schools.*")
->where("teachers.teacher_name", 'like', '%' . $search . '%')
->orWhere('school_name', 'like', '%' . $search . '%')
->orWhere('school_address', 'like', '%' . $search . '%')
->orWhere('school_focus', 'like', '%' . $search . '%');
});
uj5u.com熱心網友回復:
嘗試orWhereHas在該查詢中使用。
->when($search, function ($q) use ($search) {
$q->where('school_name', 'like', '%' . $search . '%')
->orWhere('head_teacher', 'like', '%' . $search . '%')
->orWhereHas('teachers', function ($sub) use ($search) {
$sub->where('teacher_name', 'like', '%' . $search . '%');
});
});
uj5u.com熱心網友回復:
嘗試使用雄辯的關系
School::where('school_name', 'like', '%' . $search . '%')
->orWhere('head_teacher', 'like', '%' . $search . '%')
->orWhereHas(['teachers'=>function($query) use ($search) {
return $query->where('teacher_name', 'like', '%' . $search . '%');
}]
)->get();
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/317439.html
