1

I have a one question about the laravel 4 multiple search.

I have variables that need to be checked, it is empty or not and I want to know, is there any other method to do this?

So far, I tried to do like this, but this doesn't seem good enough.

The code:

public function postQuickSearch() {

    $vehicle_manufacturer   = Input::get('vehicle_manufacturer');
    $vehicle_model          = Input::get('vehicle_model');

    $year_reg_from          = Input::get('year_reg_from');
    $year_reg_to            = Input::get('year_reg_to');

    $price_from             = Input::get('price_from');
    $price_to               = Input::get('price_to');


    if(!empty($year_reg_from) && !empty($year_reg_to)) {
        $query = Seller::where('vehicle_manufacturer', 'LIKE', '%'. $vehicle_manufacturer .'%')->orWhere('vehicle_model', 'LIKE', '%'. $this->getModel($vehicle_model) .'%')->whereBetween('year_reg', array($year_reg_from, $year_reg_to))->get();
    }

    if(!empty($price_from) && !empty($price_to)) {
        $query = Seller::where('vehicle_manufacturer', 'LIKE', '%'. $vehicle_manufacturer .'%')->orWhere('vehicle_model', 'LIKE', '%'. $this->getModel($vehicle_model) .'%')->whereBetween('price', array($price_from, $price_to))->get();
    }

    if(empty($price_from) && empty($price_to) && empty($year_reg_from) && empty($year_reg_to)) {
        $query = Seller::where('vehicle_manufacturer', 'LIKE', '%'. $vehicle_manufacturer .'%')->orWhere('vehicle_model', 'LIKE', '%'. $this->getModel($vehicle_model) .'%')->get();
    }

    if($query->count()) {

        $data = array(
            'results'       => $query
        );

        return View::make('auto.vehicle.search')->with($data);
    }

    return Redirect::route('home')->with('global', 'No results.');
}

Thanks for Your help. :)

2
  • 1
    Define "good enough". If that code is working, what problem do you need to solve? Commented Aug 8, 2014 at 11:05
  • Yes, it works, but as "user3632495" mentioned, there's so many duplications that I don't like here. That's why I asked, is there any efficient eway to do this. Thanks for Your reply. :) Commented Aug 9, 2014 at 9:48

1 Answer 1

1

Well you could remove code duplication for starters. Just try to group everything that looks alike.

Example:

public function postQuickSearch() {

    $fields = array(
        'vehicle_manufacturer','vehicle_model',
        'year_reg_from','year_reg_to',
        'price_from','price_to'
    );

    foreach($fields as $field)
        $$field = Input::get($field);

        $query = Seller::where('vehicle_manufacturer', 'LIKE', '%'. $vehicle_manufacturer .'%')
            ->orWhere('vehicle_model', 'LIKE', '%'. $this->getModel($vehicle_model) .'%');      

    if(!empty($year_reg_from) && !empty($year_reg_to)) {
        $query->whereBetween('year_reg', array($year_reg_from, $year_reg_to));
    }

    if(!empty($price_from) && !empty($price_to)) {
        $query->whereBetween('price', array($price_from, $price_to));
    }

    if(empty($price_from) && empty($price_to) && empty($year_reg_from) && empty($year_reg_to)) {
        //Nothing yet
    }

     $result = $query->get();

    if($result->count()) {

        $data = array(
            'results'       => $result
        );

        return View::make('auto.vehicle.search')->with($data);
    }

    return Redirect::route('home')->with('global', 'No results.');
}
Sign up to request clarification or add additional context in comments.

Comments

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.