Ad

Custom Query For Database In Laravel

- 1 answer

First of all I'm wondering if this is even possible in Laravel?

I have this code:

$master_array = $_POST['master_search_array'];

$count = count($master_array);
$master_string = '';

for($i=0; $i<$count; $i++) {
    if($master_array[$i] == "Dining"){
        $master_string .= "where('dining', 'dining')";
    }
    if($master_array[$i] == "Party"){
        $master_string .= "where('party','party')";
    }
    ....ETC you get the point
}

$tours = DB::table('tours')->$master_string->get();

return $tours;

So at the end I should get something like this:

$tours = DB::table('tours')->where('dining', 'dining)->where('party','party')->get();

How can I do this in laravel, it gives me an error, no matter if I pass it as $master_string or {{$master_string}}.

Ad

Answer

There is no need for master string. Just use the query builder how it's meant to be used...

    $master_array = $_POST['master_search_array'];

    $count = count($master_array);
    $query = DB::table('tours');

    for($i=0; $i<$count; $i++) {
        if($master_array[$i] == "Dining"){
            $query->where('dining', 'dining');
        }
        if($master_array[$i] == "Party"){
            $query->where('party', 'party');
        }
    }

    $tours = $query->get();

    return $tours;
Ad
source: stackoverflow.com
Ad