Nicolas
Nicolas

Reputation: 4756

SQL - Select if it contains string

I'm trying to make an sql query which picks all the trucks with the searchTerm in the name

 $foodtrucks= DB::table('foodtrucks')
        ->rightjoin('users', 'foodtrucks.user_id', '=', 'users.id')
        ->orderBy('users.premium_type','premium')
        ->select('foodtrucks.*')
        ->where('name', 'LIKE', $searchTerm)
        ->orWhere('info', 'LIKE', $searchTerm)
        ->get();

I'm currently getting the results if I write te exact name but this is not what I want to happen, I want it to display the result even it the name contains the searchTerm

so for example

What's happening now


Upvotes: 0

Views: 44

Answers (1)

Thamilhan
Thamilhan

Reputation: 13313

So you are searching for LIKE %Truck%. Go for:

$foodtrucks= DB::table('foodtrucks')
        ->rightjoin('users', 'foodtrucks.user_id', '=', 'users.id')
        ->orderBy('users.premium_type','premium')
        ->select('foodtrucks.*')
        ->where('name', 'LIKE', "%$searchTerm%")
        ->orWhere('info', 'LIKE', "%$searchTerm%")
        ->get();

Upvotes: 1

Related Questions