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
Post a Comment