L
L
LaraQuestions2020-09-11 03:04:42
Laravel
LaraQuestions, 2020-09-11 03:04:42

How to make a selection from the same table on the same columns?

Good afternoon, I would like to get an answer to the question of how to do it, or links to the article if there are any at hand

There is a table with parameters

id code value itemID
1 vodakanal yes 1
2 vodakanal no 2
3 vodakanal yes 3
4 test yes 1

How to get unique from the table itemID which

$result->where([
         ['code', '=', 'vodakanal'],
         ['value', '=', 'yes'],
     ]);


And at the same time

$result->where(function($query) {
         $query->where('code', 'test')
             ->where('value', 'yes');
     });


How to build a query correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rst630, 2020-09-11
@rst630

Table::where([
            ['value','yes'],
            [
                function($q) {
                    return $q
                        ->where('code','vodakanal')
                        ->orWhere('code','test');
                }
            ]
        ])->get();

Where Tableis the name of the model or $result- what do you have in it
. This will generate a query:
select * from `table` where (`value` = ? and (`code` = ? or `code` = ?))

Seems like what you need.
You can check which query will be the output by replacing get() with toSql(),
apparently you can simplify it like this:
Table::where('value','yes')->whereIn('code',['vodakanal','test'])->get();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question