Laravel如何使用querybuilder从两列进行组合concat

问题描述 投票:0回答:1

我有一个参数projectname,它接受基于数据库列的任何字符串。我想要的是当我搜索类似BEDOK的关键字时,它向我显示了数据库中各列的简要列表。像这样BEDOK-20BEDOK-22

我的数据库是这样的enter image description here

我尝试过的控制器代码

    public function getNames(Request $request){
        $result =  DB::connection('mysql2')
                    ->table('xp_pn_resale')
                    ->select('town')
                    ->whereRaw(DB::raw("CONCAT(town, ' ', street_name,'',block,'')LIKE '%$request->projectname%' "))
                    ->limit($request->limit)   
                    ->get();
                    // ->toSql();

        return response()->json($result);            
    }

我得到了这样的回复

"town": "BEDOK"
},
{
"town": "BEDOK"
},
{
"town": "BEDOK"
},
...

我想要的是这样

[
"town": "BEDOK-44"
},
{
"town": "BEDOK-540"
},
{
"town": "BEDOK-702"
},
...
php mysql laravel
1个回答
0
投票

您只需要这样选择:

->selectRaw('CONCAT(town, '-', block) AS town')
© www.soinside.com 2019 - 2024. All rights reserved.