0

I got a params which is projectname and it accepts any string based on the column of my db. What I want is when I search a keyword which is like this BEDOK, then shows me the concatinated list of the columns in my db. Like this BEDOK-20,BEDOK-22.

My db is like this enter image description here

My controller code i tried

    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);            
    }

And i got response like this

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

What I want is like this

[
"town": "BEDOK-44"
},
{
"town": "BEDOK-540"
},
{
"town": "BEDOK-702"
},
...

2 Answers 2

3

You just need to select like this:

->selectRaw("CONCAT(town, '-', block) AS town")
Sign up to request clarification or add additional context in comments.

1 Comment

I should replace the select with your answer?
1

I have faced this issue before and I solved it with a DB::raw only, you can try changing your whereRaw method like this

->where(DB::raw("CONCAT(`town`, '-', `block`)", "LIKE", "%".$request->projectname."%"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.