i’m kind of new to laravel first time developing on it. My goal is to get some filtered events to show, the filters should all work together (‘filter 1’ AND ‘filter2’ ecc..) even when some are empty. The problem that i’m getting is that when my filter fields are empty the query won’t return any event. My CONTROLLER code:
<?php namespace AppHttpControllers; use AppEvent; use IlluminateHttpRequest; use IlluminateSupportFacadesDB; use MockeryMatcherNot; use phpDocumentorReflectionTypesNullable; use SymfonyComponentHttpFoundationResponse; use AppRequestController; class EventsController extends Controller { public function index(){ $eventi= Event::paginate(4); return view('CatalogoEventi',compact('eventi')); } public function mostraSingoloEvento($id){ $singoloevento=Event::findOrFail($id); return view('PagineDinamiche/InformazioniEvento',['singoloevento'=>$singoloevento]); } public function searchEvent(){ $data = request('data'); $regione = request('regione'); $societa = request('società'); $desc = request('descrizione'); /* $eventi = DB::table('users'); if($desc!=null) $eventi = $eventi->where('anteprima',$desc); if($data!=null) $eventi = $eventi->where('data',$data); if($regione!=null) $eventi = $eventi->where('regione',$regione); if($societa!=null) $eventi = $eventi->where('società',$societa); return view('CatalogoEventi', ['eventi'=>$eventi]); */ $eventi = DB::table('events') ->where('anteprima','=',$desc) ->where('data', '=' , $data) ->where('regione', '=' , $regione) ->paginate(4); /* ->where('società', '=' , $societa) */ return view('CatalogoEventi',['eventi'=>$eventi]); } /* public function createEvent(){ return redirect('/CatalogoEventi'); } */ }
More in detail
public function searchEvent(){ $data = request('data'); $regione = request('regione'); $societa = request('società'); $desc = request('descrizione'); $eventi = DB::table('events') ->where('anteprima','=',$desc) ->where('data', '=' , $data) ->where('regione', '=' , $regione) ->paginate(4); return view('CatalogoEventi',['eventi'=>$eventi]); }
My view CatalogoEventi:
@extends('layouts.Public') @section('title', 'Catalogo Eventi') <!-- CatalogoEventi --> @section('content') <!-- RICERCA START --> <style> label{ color: white; font-size: 25px; background-image:image('/public/css/images/promo/p1.jpg') ; max-width: 250px; } input{ padding: 10px; font-size: 15px; max-width: 300px; } select{ padding: 10px; } </style> <div class="events parallax-one pad" id="events"> <div class="container"> <!-- default heading --> <div class="default-heading-shadow"> <!-- heading --> <h2>Catalogo Eventi</h2> </div> <!-- events element --> <div class="events-element"> <!-- BARRA DI RICERCA --> <!-- RICERCA --> <div class="container"> <!-- Collect the nav links, forms, and other content for toggling --> <form name="Ricerca" action="/grp_15f/public/CatalogoEventi/ricerca" method="POST"> @csrf <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <label for="descrizione" class="col-md-4 col-form-label text-md-right">{{ __('Descrizione ') }}</label> <input type="text" name="descrizione" id="Descrizione" placeholder="Descrizione"> <br><br> <label for="Data" class="col-md-4 col-form-label text-md-right">{{ __('Data ') }}</label> <input type="date" name="data" id="Data" placeholder="Mese ed Anno(MM/AAAA)"> <br><br> <label for="Regione" class="col-md-4 col-form-label text-md-right">{{ __('Regione') }}</label> <input type="text" name="regione" id="Regione" placeholder="Regione"> <br><br> <label for="Società" class="col-md-4 col-form-label text-md-right">{{ __('Società') }}</label> <select name="società"> <option value="NEXT EVENTI">NEXT EVENTI</option> <option value="Pincopallino srl">Pincopallino srl</option> <option value="Steposkrt spa">Steposkrt spa</option> </select> <!-- FUNZIONE RICERCA DA IMPLEMENTARE --> <input type="submit" name="tasto_cerca" value="Cerca"> </div> </form> <!-- /.navbar-collapse --> </div><!-- /.container-fluid --> <!--/ RICERCA END --> <!-- CATALOGO EVENTI --> <div class="row"> @foreach ($eventi as $event) <div class="col-md-6 col-sm-6"> <!-- event item --> <div class="events-item"> <!-- image container --> <div class="figure"> <!-- event date --> <div class="event-date"> <!-- DA MODIFICARE --> <span class="emonth">Data: {{ $event->data }}</span> <div class="clearfix"></div> </div> <!-- event location --> <span class="event-location"><i class="fa fa-map-marker"></i> Luogo:{{ $event->luogo }} </span> <!-- image --> @include('PagineDinamiche/eventImage', ['attrs' => 'imagefrm', 'imgFile' => $event->immagine]) <!-- image hover --> <div class="img-hover"> <!-- hover icon --> <a href="{{ route('infoEvento',['id'=>$event->id]) }}"><i class="fa fa-play-circle"></i></a> </div> </div> <!-- event information --> <div class="event-info"> <!-- event title --> <h3>Titolo: {{ $event->nome }}</h3> <!-- horizontal line --><hr /> <!-- paragraph --> <p> Descrizione Breve: {!! $event->anteprima !!} </p> <a href="{{ route('infoEvento',['id'=>$event->id]) }}">Scopri di piu..</a> </div> </div> </div> @endforeach </div> </div> </div> <!--Paginazione--> @include('pagination.paginator', ['paginator' => $eventi]) </div> @endsection
My routes
//CATALOGO EVENTI Route::get('/CatalogoEventi','EventsController@index') ->name('catalogoEventi'); //ROTTA RICERCA Route::any('/CatalogoEventi/ricerca','EventsController@searchEvent');
result from the query abovewith field ‘regione’ not empty and others empty
If i only work with this
$eventi = DB::table('events') ->where('regione', '=' , $regione) ->paginate(4);
actually i get some results and i can display them (with some problems referring to the paginator but it’s not the main problem). I think that i should fix the query (and i want to do this way) to get all done But for now i got no clue.
Advertisement
Answer
Your original problem is probably that the input values are not null, but rather empty strings.
But conditional queries like this are a good use of the when()
query builder method. If the first argument evaluates to a non-false value, the second argument is executed.
public function searchEvent(Request $request) { $eventi = DB::table("users") ->when($request->descrizione, fn($q, $v) => $q->where("anteprima", $v)) ->when($request->data, fn($q, $v) => $q->where("data", $v)) ->when($request->regione, fn($q, $v) => $q->where("regione", $v)) // you should rename this input to ascii just to be safe ->when($request->societa, fn($q, $v) => $q->where("societa", $v)) ->paginate(4); return view("CatalogoEventi", compact("eventi")); }
Or, if you are still on old PHP without arrow functions:
public function searchEvent(Request $request) { $eventi = DB::table("users") ->when($request->descrizione, function ($q, $v) { return $q->where("anteprima", $v); }) ->when($request->data, function ($q, $v) { return $q->where("data", $v); }) ->when($request->regione, function ($q, $v) { return $q->where("regione", $v); }) // you should rename this input to ascii just to be safe ->when($request->societa, function ($q, $v) { return $q->where("societa", $v); }) ->paginate(4); return view("CatalogoEventi", compact("eventi")); }