Answer the question
In order to leave comments, you need to log in
How to filter data across multiple queries in Laravel?
Good day to all
The task is to make a filter, as on this page - https://postupi.kz/specialnosty/
I did it and everything works. Here is the code:
Form
<form action="{{ route('filtracia') }}" method="get">
@foreach($vuz as $item)
<p>
<input type="checkbox" name="{{ $item->name }}" value="{{ $item->id }}">
{{ $item->title }}
</p>
@endforeach
<h5>Направление обучения</h5>
<hr>
@foreach($napravlenie as $value)
<p>
<input type="checkbox" name="{{ $value->alias }}" value="{{ $value->id }}">
{{ $value->title }}
</p>
@endforeach
<hr>
<button class="btn btn-success" type="submit">Подобрать</button>
</form>
if ($request->tehnical || $request->urist || $request->economic || $request->managmant || $request->gumanitar) {
$data = Specialnosti::whereIn('univer_id', [
$request->bntu,
$request->tulgu,
$request->mmu,
$request->mip,
$request->tgu,
$request->vgu,
])->whereIn('napravlenie_id', [
$request->tehnical,
$request->urist,
$request->economic,
$request->managmant,
$request->gumanitar,
])->get();
} else {
$data = Specialnosti::whereIn('univer_id', [
$request->bntu,
$request->tulgu,
$request->mmu,
$request->mip,
$request->tgu,
$request->vgu,
])->get();
}
if (!$request->bntu && !$request->tulgu && !$request->mmu && !$request->mip && !$request->tgu && !$request->vgu) {
$data = Specialnosti::whereIn('napravlenie_id', [
$request->tehnical,
$request->urist,
$request->economic,
$request->managmant,
$request->gumanitar,
])->get();
}
Answer the question
In order to leave comments, you need to log in
<input type="checkbox" name="vuz[{{ $item->name }}]" value="{{ $item->id }}">
<input type="checkbox" name="napravlenie[{{ $value->alias }}]" value="{{ $value->id }}">
You need to put name something like name="univer[]" , then in $request->univer there will be an array of selected checkboxes
Something similar to stackoverflow
You can go through the request ol and collect the required request in the builder. Type
foreach ($request->all() as $name => $value)
{
// Если название метода совпадает с названием параметра из реквеста
if (method_exists($this, $name))
{
//Запускаем методы
$this->$name($value);
}
}
//В конце возвращаем собранный билдер для дальнейшего использования
return $this->builder;
public function name($value)
{
$this->builder->where('name', 'like', "%$value%");
}
$request->tehnical
Only through input or get Oh, newbies, newbies ..)) public function scopeOfUniverById($query, $request)
{
foreach ($request->all() as $id)
{
$builder = $query->where('univer_id', $id);
}
return $builder;
}
Solved the problem. Everything turned out to be quite simple, thanks to vism for the hint. Changed the names in the view
<input type="checkbox" name="vuz[{{ $item->name }}]" value="{{ $item->id }}">
<input type="checkbox" name="napr[{{ $value->alias }}]" value="{{ $value->id }}">
$vuz_array = $request->vuz;
$napr_array = $request->napr;
if ($napr_array) {
$data = Specialnosti::whereIn('napravlenie', $napr_array)->get();
}
if ($vuz_array) {
$data = Specialnosti::whereIn('univer_alias', $vuz_array)->get();
}
if ($vuz_array && $napr_array) {
$data = Specialnosti::whereIn('univer_alias', $vuz_array)->whereIn('napravlenie', $napr_array)->get();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question