[英]Unknown column in eloquent query builder laravel 5.6
gwrcategory和gwrproces之間的關系是一對多的。 在gwrproces和gwrlist之間是多對一的。
我使用此代碼搜索數據。 我現在使用%來查找所有內容,直到可行為止。
$searchresult = GwrCategory::
whereHas('gwrproces', function($query) use ($request){
$query->where('gwr_code', 'LIKE', '%'.$request->input('gwrcode').'%')->
whereHas('gwrlist', function($query) use ($request)
{
$query->where('versie', 'LIKE', '%');
}
)->with('gwrlist')->get();
}
)->with('gwrproces')->get();
上面代碼的結果sql是:
select * from `gwr_proces`
where `gwr_proces`.`gwr_category_id` = `gwr_categories`.`id` and `gwr_code` LIKE %20%
and exists
(select * from `gwr_lists`
where `gwr_proces`.`gwrlist_id` = `gwr_lists`.`id` and `versie` LIKE %)
這將產生錯誤:SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ gwr_categories.id”。
我玩過phpmyadmin中的查詢,這似乎使它正常工作。 我只將gwr_categories
添加到第一個select * from。
select * from `gwr_proces`,`gwr_categories`
where `gwr_proces`.`gwr_category_id` = `gwr_categories`.`id` and `gwr_code` LIKE '%'
and exists
(select * from `gwr_lists`
where `gwr_proces`.`gwrlist_id` = `gwr_lists`.`id` and `versie` LIKE '%')
我的問題是:我怎樣才能說服雄辯地添加正確的列或重寫代碼以使其在gwrproces和gwrlist表中搜索列中的特定文本,同時又保持與類別的關系完整?
編輯:我使用渴望加載,因此with()-> get()
問題是您執行了嵌套查詢。
您也不能像這樣使用緊急加載。
刪除->with('gwrlist')->get()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.