Noman Ali Noman Ali - 2 months ago 13
MySQL Question

Laravel query variable as string

What i am doing here might be stupid but i am newbie to laravel,
I am applying search filters for database using a query.
Query is being built using javascript and being passed to laravel function.
//types contains a string like "international#global#europe# etc

Now in Controller i am getting that string
and exploding it to make a query like this

$tquery = "Where('type',".$typesArray[0].")";
for($offset=1; $offset < count($typesArray); $offset++) {
$tquery .= "->orWhere('type', ".$typesArray[$offset].")";

and executing query like this

$firms = Firm::$tquery->get();

it gives me error

Access to undeclared static property: App\Firm::$tquery

How can i do what i want to.

PS. types are checkboxes some of them can be checked or all.


It's better to use whereIn() method:

$firms = Firm::whereIn('type', $typesArray)->get();

But if you need to use your way, you can do something like this:

$firm = new App\Firm;

foreach ($typesArray as $type) {
    $firm = $firm->orWhere('type', $type);

$firms = $firm->get();