Query problem using DB Where/orWhere Laravel

Asked

Viewed 226 times

1

Good afternoon friends, I’m having problems with a query using DB in the Windows,

I put a clause Where and another orWhere and a whereBetween, but it is not going through the between, and when shooting the orWhere works, someone has already gone through it?

Code:

$result = DB::table('tb_monitor_laudo_info')
            ->where('usuariox_operador', 'like', $usuariox)
            ->orWhere('id', '=', $usuariox)
            ->whereBetween('data_laudo',[$datainicial, $datafinal])
            ->orderBy('operador')
            ->get();

1 answer

3


It is necessary to use a Parameter Grouping of the Laravel, so that the ORM does not get confused in the relation between the operators, see below how it should be:

$result = DB::table('tb_monitor_laudo_info')
            ->where('usuariox_operador', 'like', $usuariox)
            ->where(function ($query) use ($usuariox) {
               $query->where('usuariox_operador', 'like', $usuariox)
                 ->orWhere('id', '=', $usuariox);
             })
            ->whereBetween('data_laudo',[$datainicial, $datafinal])
            ->orderBy('operador')
            ->get();
  • 1

    It worked!! vlw man

Browser other questions tagged

You are not signed in. Login or sign up in order to post.