php - Laravel-5 search certain fields if user input is not empty -


i'm trying build search query using laravel-5 seem generating wrong sql.

here's code using:

bookingdates::where('status', 'booked')->where('email', input::get('email'))->where('name', 'like', '%' . input::get('name') . '%')->where('date', $date)->get(); 

however user may not provide email. when happens, part of query (where('email', input::get('email'))) should omitted. example, query run if didn't provide email:

bookingdates::where('status', 'booked')->where('name', 'like', '%' . input::get('name') . '%')->where('date', $date)->get(); 

is there easy/pretty way using laravel-5?

thanks.

you can build query in separate steps:

$query = bookingdates::where('status', 'booked')                      ->where('name', 'like', '%' . input::get('name') . '%')                      ->where('date', $date);  if($email = input::get('email')){     $query->where('email', $email); }  $result = $query->get(); 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -