Laravel 搜索有很多路径
我想根据搜索关键字显示所有具有相同Kantor的kelompoks 我有三张桌子 KantorLaravel 搜索有很多路径,laravel,model,laravel-6,Laravel,Model,Laravel 6,我想根据搜索关键字显示所有具有相同Kantor的kelompoks 我有三张桌子 Kantor id nama id nama id_kantor id nama id_gugus Gugus id nama id nama id_kantor id nama id_gugus Kelompok id nama id nama id_kantor id nama id_gugus 这是我的控制器: $kelompok=kelompok::wi
id
nama
id
nama
id_kantor
id
nama
id_gugus
Gugus
id
nama
id
nama
id_kantor
id
nama
id_gugus
Kelompok
id
nama
id
nama
id_kantor
id
nama
id_gugus
这是我的控制器:
$kelompok=kelompok::with('bidang', 'kantor')->whereHas('kantor', function($q) use ($key) {
$q->where('nama', 'like', '%'.$key.'%');
})->orWhereHas('bidang', function($q) use ($key) {
$q->where('nama', 'like', '%'.$key.'%');
})->orWhere('nama', 'like', '%'.$key.'%')->get();
我的模型是:
Kelompok.php
public function bidang()
{
return $this->belongsTo('App\gugus', 'id_gugus');
}
public function kantor()
{
return $this->hasManyThrough('App\kantor', 'App\gugus', 'id_kantor', 'id_gugus', 'id', 'id');
}
Gugus.php
public function bidang()
{
return $this->belongsTo('App\gugus', 'id_gugus');
}
public function kantor()
{
return $this->hasManyThrough('App\kantor', 'App\gugus', 'id_kantor', 'id_gugus', 'id', 'id');
}
公共功能坎特()
{
返回$this->belongsTo('App\kantor','id\u kantor');
}
Kantor
public function gugus()
{
return $this->hasMany('App\gugus', 'id_kantor');
}
以下是我当前获得的输出(错误):
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'nama' in where clause is ambiguous (SQL: select * from `kelompoks` where exists (select * from `kantors` inner join `guguses` on `guguses`.`id` = `kantors`.`id_gugus` where `kelompoks`.`id` = `guguses`.`id_kantor` and `nama` like %gov%) or exists (select * from `guguses` where `kelompoks`.`id_gugus` = `guguses`.`id` and `nama` like %gov%) or `nama` like %gov%)
对不起,我的英语不好,希望你能理解:D我已经解决了它,我最终使用了嵌套wherehas,不漂亮,但完成了任务
$kelompok=kelompok::with('bidang')->whereHas('bidang', function($q) use ($key) {
$q->where('nama', 'like', '%'.$key.'%');
})->orWhereHas('bidang', function($q) use ($key){
$q->whereHas('kantor', function($q) use ($key){
$q->where('nama', 'like', '%'.$key.'%');
});
})->orWhere('nama', 'like', '%'.$key.'%')->get();
尝试使用
tablename.columnname
而不是只使用列名,它应该可以正常工作。在哪里?请您详细说明,谢谢作为答案,尝试使用我在回答中所做的那样,并告诉我,如果您仍然面临任何问题。