user2132802
user2132802

Reputation: 27

Laravel 4 multiple field search

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. :)

Upvotes: 1

Views: 506

Answers (1)

user3632495
user3632495

Reputation: 149

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.');
}

Upvotes: 1

Related Questions