i need help
i need help

Reputation: 2386

Eloquent full name search not returning result

here's my table data

firstname lastname
boy 5
boy 55
boy 6
boy 7

here's my codes inside a search function

    $search = $request->search; 
    $users = \DB::table('users')
    ->where(function($query) use ($search){
        $query->where('firstname', 'like', '%'.$search.'%');
        $query->orWhere('lastname', 'like', '%'.$search.'%');
        $query->orWhereRaw(" concat(firstname, ' ', lastname) like '%?%' ", [$search]);
        $query->orWhere('email', 'like', '%'.$search.'%');
    })
    ->orderBy('firstname', 'asc')
    ->limit(20)
    ->get();

type in boy to press search, a bunch of matching result on firstname will return

type in 5 to press search, both the 5 and 55 row data will return

type in the full name "boy 5" to press search, no result return.

from my research, using orWhereRaw with concat and binding in this way should be getting result. however, things doesn't work.

is there anything wrong ? btw, am running this in laravel 7.

Upvotes: 0

Views: 301

Answers (2)

It's VIN
It's VIN

Reputation: 312

check out the below example and adjust your query function accordingly

//what is provided from search bar
$query = $request->input('query');

//db tbl that gets queried
$spa = spa::latest()->where('spa_name', 'LIKE', '%'.$query.'%')
                    ->orwhere('more_details','LIKE',  '%'.$query.'%')->get();

Upvotes: 0

Peppermintology
Peppermintology

Reputation: 10210

Replace

$query->orWhereRaw(" concat(firstname, ' ', lastname) like '%?%' ", [$search])

With

$query->orWhereRaw("concat(firstname, ' ', lastname) like ?", ['%'.$search.'%'])

Upvotes: 2

Related Questions