简体   繁体   English

雄辩的查询生成器laravel 5.6中的未知列

[英]Unknown column in eloquent query builder laravel 5.6

The relationship between gwrcategory and gwrproces is one to many. gwrcategory和gwrproces之间的关系是一对多的。 Between gwrproces and gwrlist is many to one. 在gwrproces和gwrlist之间是多对一的。

I use this code to search in the data. 我使用此代码搜索数据。 I use % for now to find everything until this works. 我现在使用%来查找所有内容,直到可行为止。

    $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();

The resulting sql of the code above is: 上面代码的结果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 %)

This gives an error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'gwr_categories.id' in 'where clause'. 这将产生错误:SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ gwr_categories.id”。

I've played around with the query in phpmyadmin and this seems to get it working. 我玩过phpmyadmin中的查询,这似乎使它正常工作。 I only added gwr_categories to the first select * from. 我只将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 '%')

My question is: how can I convince eloquent to add the correct column or rewrite the code to get it to search in gwrproces and gwrlist tables for a specific text in a column while keeping the relationship with categories in-tact? 我的问题是:我怎样才能说服雄辩地添加正确的列或重写代码以使其在gwrproces和gwrlist表中搜索列中的特定文本,同时又保持与类别的关系完整?

Edit: I use eager loading, hence the with()->get() 编辑:我使用渴望加载,因此with()-> get()

The problem is that you execute the nested query. 问题是您执行了嵌套查询。
You also can't use eager loading like that. 您也不能像这样使用紧急加载。

Remove ->with('gwrlist')->get() . 删除->with('gwrlist')->get()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM