F
F
fvg2015-06-17 13:01:18
ORM
fvg, 2015-06-17 13:01:18

Is there a way to cancel any condition in QueryBuilder (Laravel 5)?

Suppose there is such a request At some point in time, before the moment of sampling from the database, it is necessary to cancel the condition on the user's age. Here's how to do it?
$query = DB::table('users')->whereAge(18);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
fvg, 2015-06-17
@fvg

I found the answer myself :)

foreach ((array) $query->wheres as $key => $where)
    {
        if ($where['column'] == 'age')
        {
            unset($query->wheres[$key]);

            $query->wheres = array_values($query->wheres);
        }
    }

E
Evgeniy Odinets, 2015-06-17
@evgeniy2194

$query = DB::table('users');
if(){
     //code
} else {
$query = $query->whereAge(18);
}
$query= $query->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question