Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/styllloz/public_html/qa-include/qa-base.php on line 1175

Warning: session_start(): Cannot start session when headers already sent in /home/styllloz/public_html/qa-include/app/users.php on line 162

Warning: Cannot modify header information - headers already sent by (output started at /home/styllloz/public_html/qa-include/qa-base.php:1175) in /home/styllloz/public_html/qa-include/app/users.php on line 1267

Warning: Cannot modify header information - headers already sent by (output started at /home/styllloz/public_html/qa-include/qa-base.php:1175) in /home/styllloz/public_html/qa-include/app/page.php on line 356
How to filter data based on multiple queries in Laravel? - code-flow.club | Q&A

How to filter data based on multiple queries in Laravel?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
9 views
Good day to all
The task is to make the filter like on this page https://postupi.kz/specialnosty/
I did and it works. Here is the code:
Form
@foreach($vuz as $item) {{ $item->title }} @endforeach Field of study@foreach($direction as $value) {{ $value->title }} @endforeach 

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 it works only with static data. Ie, if you add a UNIVERSITY or specialty, you'll have to manually add code in the controller. How do I filter (DB query) to work without editing the controller?

Many thanks for any help.
by | 9 views

4 Answers

0 like 0 dislike
\r


from this, and we dance. Right here is the code to write for you I'm lazy.
by
0 like 0 dislike
Need name put something like name="univer[]" , then $request->univer will be an array of the selected checkbox's

Something similar to stackoverflow
by
0 like 0 dislike
You can walk foritem at the request ol and collect in bilder you need. Type
foreach ($request->all() as $name => $value) { // If a method name coincides with the name of the parameter from request if (method_exists($this, $name)) { //Run methods $this->$name($value); } } //At the end is returned assembled by the Builder for further use return $this->builder;

what methods of filters to make the type
public function name($value) { $this->builder->where('name', 'like', "%$value%"); }

this, of course, if I understood correctly what you need. In General as ideas) But to list the entire request, not of course need to refactor torment. Plus it is impossible to request apply here $request->tehnical Only via input or a get eh beginners beginners..))

If only the model controller without a separate class - it is another option :
Doing in the model Specialnosti osprey (names must start with scope)
public function scopeOfUniverById($query, $request) { foreach ($request->all() as $id) { $builder = $query->where('univer_id', $id); } return $builder; }

and in the controller
$spec = Specialnosti::ofUniverById($request)->get();

I have not tested but in theory should work)
by
0 like 0 dislike
The problem was solved. Everything was pretty simple, thanks to vism for the tip. In view of the changed names
\r

And in the controller wrote
$vuz_array = $request->vuz; $napr_array = $request->napr; if ($napr_array) { $data = Specialnosti::whereIn('direction', $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('direction', $napr_array)->get(); }

Works perfectly. Thank you all for the help
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
5 answers
110,608 questions
257,186 answers
0 comments
35,181 users