【问题标题】:Controller concat problems控制器连接问题
【发布时间】:2019-05-09 13:08:40
【问题描述】:

我在使用 ajax 获取选择选项时遇到问题:

“SQLSTATE[42S22]: 找不到列: 1054 'where 子句'中的未知列'full_name' (SQL: select count(*) as aggregate from drivers where full_name like %% ▶”

public function drivers(Request $request)
    {
        $q = $request->get('q');

        return Driver::select("id", "first_name", "last_name"
                ,DB::raw("CONCAT(first_name,' ',last_name) as full_name"))
            ->where('full_name', 'like', "%$q%")->paginate(null, ['id', 'full_name as text']);
    }

不知道问题出在哪里,有什么可能的解决办法吗?

谢谢

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您不能在where 子句中使用虚拟列或别名。所以试试这个吧:

    public function drivers(Request $request)
    {
        $q = $request->get('q');
    
        return Driver::select("id", "first_name", "last_name",
               DB::raw("CONCAT(first_name,' ',last_name) as full_name"))
               ->having('full_name', 'like', "%$q%")->simplePaginate(10);
    }
    

    【讨论】:

    • "SQLSTATE[42S22]: 找不到列:1054 未知列 'full_name' in 'have Clause' (SQL: select count(*) as aggregate from drivers with full_name like ▶"
    • 您能否删除您的paginate 呼叫并重试。我现在才注意到这一点。我编辑了我的代码。
    • tahnks 你,没有分页工作正常,但我需要分页
    • 你使用了错误的分页,你又改变了 full_name 的别名,所以现在试试我的代码。
    • 我试过你的代码,但同样的错误:“SQLSTATE[42S22]: Column not found: 1054 Unknown column 'full_name' in 'have Clause' (SQL: select count(*) as aggregate from @987654326 @有full_name喜欢▶"
    【解决方案2】:

    为什么不这样尝试

    return Driver
        ::select("id", "first_name", "last_name",DB::raw("CONCAT(first_name,' ',last_name) as full_name"))
        ->whereRaw('full_name like %?%', [$q])
        ->paginate(null, ['id', 'full_name as text']);
    

    https://laravel.com/docs/5.8/queries 原始方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多