V
V
Vladimir Kokhan2019-01-26 12:07:13
Laravel
Vladimir Kokhan, 2019-01-26 12:07:13

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>

Controller
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();
        }

But this only works with static data. That is, if you add a university or specialty, you will have to manually add the code in the controller. How to properly organize filtering (a query to the database) so that it works without editing the controller?
Thanks a lot for any help.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
vism, 2019-01-26
@SkazochNick

<input type="checkbox" name="vuz[{{ $item->name }}]" value="{{ $item->id }}">
<input type="checkbox" name="napravlenie[{{ $value->alias }}]" value="{{ $value->id }}">

from this and dance. I'm too lazy to write the code for you.

A
Alexey Bille, 2019-01-26
@alexey_bille

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

J
jazzus, 2019-01-26
@jazzus

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;

well, make filter methods like
public function name($value)
  {
     $this->builder->where('name', 'like', "%$value%");
  }

This, of course, if I understand correctly what you need. In general, as an idea) But, of course, you don’t need to list the entire request, you are tormented by refactoring. Plus, you can’t access the request like this $request->tehnicalOnly through input or get Oh, newbies, newbies ..))
If only with a model, a controller without a separate class, then another option:
Make ospreys in the Specialnosti model (names should begin with scope)
public function scopeOfUniverById($query, $request)
    {
      foreach ($request->all() as $id)
          {
              $builder = $query->where('univer_id', $id);
          }
        return $builder;
    }

and in the controller
I haven't tested it, but it should work

V
Vladimir Kokhan, 2019-01-29
@SkazochNick

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 }}">

And in the controller I wrote
$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();
        }

Works great. Thanks everyone for the help

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question