3

I have a database of vacation tours and want to sort it by 3 parameters at the same time (country of destination, departure-date, hotel).

So I created three inputs in html form (destination, date, hotel)

<form action="/search-tours" method="GET">
<input name="destination" type="text">
<input name="date" type="date">
<input name="hotel" type="text">
<button type=submit>Search tours</button>

I am using Laravel, so my php code is in controller ("Tour" is the model = connection to a table "tours" in the DB).

        $destination = $request->get('destination');
        $date = $request->get('date');
        $hotel = $request->get('hotel');


        $tours = Tour::where('destination', '=', $destination)
                      ->where('departure', '=' , $date)
                      ->where('hotel', '=', $hotel)
                     ->get();

It gives correct results when I submit all the inputs, but when any is missing it doesn't work at all.

I thought I could solve it via If-else, but then I realised that there can be 7 situations (1- parameter present, 0 - missing: for ex destination(1)-date(0)-hotel(0) = 100, and total: 111, 100, 010, 001, 110, 011, 101 - we don't care about 000). And if we have 4 inputs it would be 15, 5 params - 31 etc.

So it would be tedious to write so many if-else's, then it means there is another appoach.

What approach should I take to make it work if only 1 or 2 inputs was given?

3
  • 1
    I removed some excuses from your post, because it is not a crappy question. :) Commented Apr 27, 2017 at 21:25
  • 1
    actually, can you do something like stackoverflow.com/a/14179817/576767 Commented Apr 27, 2017 at 21:28
  • thank you Felix) maybe you can change the Subject so it would be more helpful for people with the same problem?) (so they can find it easier via search, cause I couldn't find anything). Commented Apr 27, 2017 at 22:05

1 Answer 1

2
$query = (new Tour)->newQuery();
if($request->has('destination')){
    $query->where('destination', '=', $request->get('destination'));
}
if($request->has('date')){
    $query->where('departure', '=', $request->get('date'));
}
if($request->has('hotel')){
    $query->where('hotel', '=', $request->get('hotel'));
}
$tours = $query->get();
Sign up to request clarification or add additional context in comments.

4 Comments

thanks, but when I hit "submit" it gives "Non-static method Illuminate\Database\Eloquent\Model::newQuery() should not be called statically", what should I update?:)
Thank you, Dan! I would have spended ages to find this option if not you.
Well newQuery() just returns eloquent query builder object. You can trace it in source code if you want to find out how it actually happens. Here's also an article (one of many) with examples of query building (filtering): m.dotdev.co/…
Thanks, the article is great, I've read it to the end but understood only the first half. Will keep on learing and hope one day i'll be able to understand it completely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.