Use the OR clause (||) in Eloquent queries in Laravel

2

Greetings community I have a doubt and I do not know how it is resolved ...

How well we know if I wanted to make a query that had an OR clause in SQl would be something like this:

Select * tabla where campoA='1' || campoA='2'

The issue is that I'm doing it in Laravel 5.2 using eloquent and according to the documentation I should use the orWhere.

$variable=Tabla::orderBy('id')
->where('campoA','=','1')
->orWhere('campoA','=','2')
->get();

The problem is that I've done this and what I get is not the expected result, it gives me another kind of result, so I do not know what I'm doing wrong, since I've been guided by the documentation, of being wrong done, what is the correct way to use this option?

I hope you can help me.

    
asked by jose angarita 06.08.2018 в 22:17
source

3 answers

1

It seems to me that the error originates when invoking groupBy() before the clauses where() .

Solution

The query should have the following form:

$variable=Tabla::where('campoA','=','1')
    ->orWhere('campoA','=','2')
    ->orderBy('id')
    ->get();

Alternative

Since I see that the value you're looking for is in a range, you can use the whereIn () to indicate a range of values for a certain key:

$variable=Tabla::whereIn('campoA', ['1','2'])
    ->orderBy('id')
    ->get();

The documentation explains it better:

  

whereIn / whereNotIn

     

The whereIn method verifies that the value of a given column is   find within the values of the last array as second parameter:

$users = DB::table('users')
                ->whereIn('id', [1, 2, 3])
                ->get();
     

The whereNotIn method verifies that the value of a given column is not   find within the values of the last array as second parameter:

$users = DB::table('users')
                ->whereNotIn('id', [1, 2, 3])
                ->get();
    
answered by 06.08.2018 / 23:29
source
1

Assuming that your class that represents the table where you should make the query is called a table, you can do it this way:

$variable=  Tabla::where('campoA','1')
    ->orWhere('campoA', '2')
    ->orderBy('id')
    ->get();

In the above you can omit the second parameter that you had the = since it is by default the type of comparison to use. You can do it in this other way too:

$variable = DB::table('tu_table')
    ->where('campoA','1')
    ->orWhere('campoA', '2')
    ->orderBy('id')
    ->get();

Verify that in your table you really have records with those values.

    
answered by 06.08.2018 в 22:49
0

I greet you and tell you that using Eloquent your query can be as follows

CODE THAT SOLVES THE QUESTION

$data = Tabla::select('*')
    ->where('campoA', '=', '1')
    ->orWhere('campoA', '=', 2)
    ->orderBy('id')
    ->get();
  

Fully respecting the wildcard operator% co_of% that you are using to   bring all the data, perfectly after invoking the model    * that you access the static method select, for   within this same you pass the operator Tabla en este caso and subsequently   by method chaining or chain of methods, both   where, first the main one and then the one that carries the logical operator    * you finally close your query with the or method so that you   return your data

With the above you will be able to continue using Eloquent and you avoid using Fluent, which is the queryBuilder

The orderBy () method can have the following structures

->orderBy('id');
->orderBy('id', 'DESC');

EXPLANATION

  • Table is the equivalent name of the model you are using
  • the get(); method will receive the wildcard operator select() or the name of the specific columns you want to query
  • first goes the method *
  • later, given the query logic, the method where
  • Query source

    link

    For the case of the consultations with the use of the different methods orWhere() , the following link of the official documentation will be useful to you

    link

        
    answered by 06.08.2018 в 23:21