Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mkumar2001's avatar

Laravel query

Dear friends, below code is working fine in mysql. I want this code to be converted for Laravel so guys please help if anyone. SELECT * FROM tablename WHERE (field1=1 AND (field2=32 or field3=8) AND (field4 LIKE "%6562%" OR field5 LIKE "%6562%")) Thanks in advance

0 likes
14 replies
cujo's avatar
$tableQuery
    ->where(function ($query) { 
        return $query->where('field2', 32)->orWhere('field3', 8); 
    })
    ->where(function ($query) {
        //similar to above
    });

I'm not sure it return is necessary in this case. If values (32, 8, etc) are stored in variables remember to pass them to function:

$field2Value = 32;
$field3Value = 8;

$tableQuery
    ->where(function ($query) use ($field2Value, $field3Value) { 
        return $query->where('field2', $field2Value)->orWhere('field3', $field3Value); 
    })
    ...
Cobs's avatar

WHERE blocks are made with closure in a where method

$query = MyModel::query() // or DB::table('tablename ')
$query->where(function($q1) {
    $q1->where('field1', 1);
    $q1->where(function($q2) {
        $q2->where('field2', 32);
        $q2->orWhere('field3', 8);
    });
    $q1->where(function($q3) {
        $q3->where('field4', 'LIKE', '%6562%');
        $q3->orWhere('field5 ', 'LIKE', '%6562%');
    });
});
cujo's avatar

At the end you should finish chain with get() method to retrieve records.

So:

$results = $tableQuery-> ... ->get();

Don't know if how much do you know about Laravel but take a look at https://laravel.com/docs/5.8/eloquent.

Outside of eloquent models you can use something like this:

use DB;

...

$results = DB::table('tableName')
    ->where(...)
    ->where(...)
    ->get();
mkumar2001's avatar

I have converted this as below- $location="8"; $keywords="6562"; $lst = listing::whereRaw('find_in_set(?,country_id)',[$cid]) ->where(function ($query) use ($location) { $query->where('state', $location)->orWhere('city', $location); }) ->where(function ($query) use ($keywords) { $query->where('title','LIKE', '%'.$keywords.'%')->orWhere('listing_id', 'LIKE', '%'.$keywords.'%'); }) ->paginate(20); but getting blank as result

mkumar2001's avatar

@AUGUST - $location="8"; $keywords="6562"; $lst = listing::whereRaw('find_in_set(?,country_id)',[$cid]) ->where(function ($query) use ($location) { $query->where('state', $location)->orWhere('city', $location); }) ->where(function ($query) use ($keywords) { $query->where('title','LIKE', '%'.$keywords.'%')->orWhere('listing_id', 'LIKE', '%'.$keywords.'%'); }) ->paginate(20);

mkumar2001's avatar

@COBS - this returns error. I have converted as below- $location="8"; $keywords="6562"; $lst = listing::whereRaw('find_in_set(?,country_id)',[$cid]) ->where(function ($query) use ($location) { $query->where('state', $location)->orWhere('city', $location); }) ->where(function ($query) use ($keywords) { $query->where('title','LIKE', '%'.$keywords.'%')->orWhere('listing_id', 'LIKE', '%'.$keywords.'%'); }) ->paginate(20);

still no result.

mkumar2001's avatar

Can I use same code- SELECT * FROM tablename WHERE (field1=1 AND (field2=32 or field3=8) AND (field4 LIKE "%6562%" OR field5 LIKE "%6562%"))

In laravel

cujo's avatar

@mkumar2001 please format the code that you're posting. It's hardly readable in format that you provided.

Why are you using field_in_set?

$cid = ?;
$location="8";
$keywords="6562";

$lst = listing::whereIn('county_id', [$cid])
    ->where(function ($query) use ($location) { 
        $query->where('state', $location)
            ->orWhere('city', $location);
    })->where(function ($query) use ($keywords) { 
        $query->where('title','LIKE', '%'.$keywords.'%')
            ->orWhere('listing_id', 'LIKE', '%'.$keywords.'%'); 
    })->paginate(20);

//Edit: There is no error during request right?

cujo's avatar

@mkumar2001 Let me see how listing model looks like, and how you're trying to display the data.

mkumar2001's avatar

@AUGUST - $location=$filters['location']; $keywords=$filters['keywords']; $lst = listing::whereRaw('find_in_set(?,country_id)',[$cid]) ->where(function ($query) use ($location) { $query->where('state', $location) ->orWhere('city', $location); }) ->where(function ($query) use ($keywords) { $query->where('title','LIKE', '%'.$keywords.'%') ->orWhere('listing_id', 'LIKE', '%'.$keywords.'%'); }) ->paginate(20); dd($lst); it returns either title or listing_id or simetimes blank however there is one row.

jlrdw's avatar

Please properly format your code.

And there are literally thousands of query Builder and eloquent query examples.

Please or to participate in this conversation.