0

I want to build a query in MySQL with this statement:

SELECT CONCAT(students.FirstName, ' ', students.LastName) AS FullName FROM `students` WHERE CONCAT(students.FirstName, ' ', students.LastName)LIKE '%John%'

And now, I use Query Builder from Laravel where I can match the column using CONCAT(). Here's my code for reference:

    public function getStudent(Request $request)
    {
        $fullName = Student::select(FacadesDB::raw('SELECT CONCAT(LastName, ", ", FirstName) AS FullName'), )
            ->where(FacadesDB::raw('CONCAT(LastName, ", ", FirstName)', 'LIKE', "'% {{ $request->terms }} %'"))
            ->get();
        return response()->json($fullName);
    }

When I try to run the code, I got a result like this:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT CONCAT(LastName, ", ", FirstName) AS FullName from students where CO...' at line 1 (SQL: select SELECT CONCAT(LastName, ", ", FirstName) AS FullName from students where CONCAT(LastName, ", ", FirstName) is null)

Is there a way that I can achieve this one? It took me like almost 4 hours to solve this code and I still can't solve it. Every help would be appreciated. I'm a new Laravel user, btw.

2 Answers 2

1

you can use map function:-

Student::select("firstname","lastname")
->where("firstname","like","%john%")
->orWhere("lastname","like","%john%")
->get()->map(function($query){
    $fullname = $query->firstname." ".$query->lastname
    return ["fullname" => $fullname]
 })
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the tip, dude. This is similar to what I solved lately. I'll try this one.
0

I would keep it simple and just phrase your query as:

SELECT CONCAT(FirstName, ' ', LastName) AS FullName
FROM students
WHERE FirstName LIKE '%John%' OR LastName LIKE '%John%';

Updated Laravel code:

public function getStudent(Request $request)
{
    $fullName = Student::select(FacadesDB::raw('CONCAT(LastName, ", ", FirstName) AS FullName'), )
        ->where(FirstName, 'LIKE', '%John%')
        ->orWhere(LastName, 'LIKE', '%John%')
        ->get();
    return response()->json($fullName);
}

4 Comments

I still have an error like this: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'SELECT CONCAT(LastName, ", ", FirstName)' in 'field list'
Try removing SELECT from the input to the raw function.
I remove SELECT and still got this error SQLSTATE[42S22]: Column not found: 1054 Unknown column 'CONCAT(LastName, ", ", FirstName)' in 'field list'
Check the docs for making a raw query. The SQL query I gave you at the top of my answer should work fine.

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.