dimanche 17 juin 2018

Laravel Query Builder - where clause equals anything programmatically

I'm using Laravel 5.6 - Query Builder.

Is it possible to make a query builder where statement that a value equals everything programmatically?

Let's say that I have this code:

$foo = 1;

DB::table('users')
  ->select('*')
  ->where('status', '=', $foo)
  ->get();

If $foo = 1 then it's straightforward. The query will select everything with the status of 1.

Q: Is it possible to assign something to the $foo variable so the select query returns every record regardless of the status from the DB?

Of course, I can make it happen with 2 query statements like this:

$foo = 1;

if ($foo === null) {
  DB::table('users')
    ->select('*')
    ->get();
} else {
  DB::table('users')
    ->select('*')
    ->where('status', '=', $foo)
    ->get();
}

However, I'm looking for a shorter / more effective solution. Is it possible somehow - without using raw code inside the Where statement?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire