0

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}}.

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

1 Comment

This did not even cross my mind, i always complicate things ^^ thank you
0

Use the ability to add wheres to the query along the way.

DB::table('tour')->where(function($query) use ($master_array)
{
    foreach($master_array as $k => $v) {

        if($v == "Dining"){
            $query->where('dining','dining');
        }
        if($v == "Party"){
            $query->where('party','party');
        }
    }
})
->get();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.