Запрос Laravel, где в столбце должно быть указано несколько переменныхPhp

Кемеровские программисты php общаются здесь
Ответить
Гость
 Запрос Laravel, где в столбце должно быть указано несколько переменных

Сообщение Гость »


I dont know how to describe this problem. Lets say i have this table in my database:

Код: Выделить всё

| Team | Zone |
| ---- | ---- |
| Alfa | A    |
| Alfa | BB   |
| Alfa | CCC  |
| Beta | A    |
| Beta | CCC  |
| Beta | DDDD |
| Crit | BB   |
| Crit | CCC  |
And then I make some form in my view, that can search the team name based on zone. The form looks like this
Изображение
What i wanted are:
  • If i enter 'A' and 'CCC' in the said form, it will show team Alfa and Beta
  • If i enter 'BB' and 'CCC' in the said form, it will show team Alfa and Crit
  • If i enter only 'BB' in the said form, it will show team Alfa and Crit
The problem is, i haven't figured out the query or the code to get that result. I have tried the following code:

Код: Выделить всё

$find = DB::table('team_zone_area')->select(
'team'
);

if(isset($req->search_zones)){
$find = $find->where('zone', $req->search_zones[0]);

if(count($req->search_zones) > 1){
for($j = 1; $j < count($req->search_zones); $j++){
$find = $find->where('zone', $req->search_zones[$j]);
}
}
}

$find = $find->whereNull('deleted_at')
->groupBy('team')
->get();
It show zero result if i search for zone 'A' and 'CCC', or 'BB' and 'CCC', but it will show correct team if i just enter zone 'BB'
Then i tried to tweak the code

Код: Выделить всё

if(count($req->search_zones) > 1)
into this:

Код: Выделить всё

if(count($req->search_zones) > 1){
for($j = 1; $j < count($req->search_zones); $j++){
$find = $find->orWhere('zone', $req->search_zones[$j]);
}
}
Now it shows all teams (Alfa, Beta, Crit) when i enter 'A' and 'CCC', instead of just Alfa and Beta
Anyone have idea what the query supposed to be?


Источник: https://stackoverflow.com/questions/781 ... e-variable
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Php»