vendredi 28 avril 2017

Laravel 5.3 Post search with session for complex search

i have a search routes like that

Route::post('search', 'SearchController@index');
Route::get('search', 'SearchController@index');

now i'm searching the result from home page form etc 6 form fields then it redirect me on the search page with result but when i click it second time on pagination page it does not work for me.i'm using session in it for remember data that i post for the home. i can search also on this page if i have session then i read second condition in my case.

Controller is below

public function index(Request $request) {

    if( Request::get('select_menu') !='' ){

                $conditions = array();
                $select_menu = Request::get('select_menu');
                $conditions['property_type'] = $select_menu;
                $city =  Request::get('city');
                $area =  Request::get('area');
                if($city != 0){
                    if($area != 0){
                        $get_area = Area::where('id', $area)->first();

                        $conditions['city'] = $get_area['city'];

                        $conditions['area'] = $get_area['area_name'];
                    }
                    else{
                        $get_city = Area::where('city_id', $city)->first();
                        $conditions['city'] = $get_city['city'];
                    }

                }
                $status =  Request::get('status');
                $ptype = Request::get('ptype');
                if($status != '0'){
                    $conditions['purpose'] = $status;
                }
                if($ptype != '0'){
                   $conditions['property_sub_type'] = $ptype;
                }

                $min_price =  Request::get('minprice');
                $max_price = Request::get('maxprice');
                if($max_price == 0){
                    $max_price = 500000000;
                }                
                $results = Property::where($conditions)->whereBetween('price', array($min_price, $max_price))->paginate(6);

                $formData = array(
                                    'city'     =>  $city,
                                    'area'     =>  $area,
                                    'status'  =>  $status,
                                    'minprice' =>  $min_price,
                                    'maxprice'  =>  $max_price,
                                    'ptype'     =>$ptype,
                                    'select_menu'=>$select_menu,
                                );
                //$results->appends(['search' => $formData]);
                Session::set('formDataSession', $formData);

            }
        elseif(Session::has('formDataSession')){

                $getSession = Session::get('formDataSession');

                $conditions = array();
                $conditions['property_type'] = $getSession['select_menu'];
                $conditions['city'] = $getSession['city'];
                $conditions['area'] = $getSession['area'];
                $conditions['purpose'] = $getSession['status'];
                $conditions['property_sub_type'] = $getSession['ptype'];
                $min_price = $getSession['minprice'];
                $max_price = $getSession['maxprice'];

                $results = Property::where($conditions)->whereBetween('price', array($min_price, $max_price))->paginate(6);
                echo $results;
                $formData = array(
                                    'city'     =>  $getSession['city'],
                                    'area'     =>  $getSession['area'],
                                    'status'  =>   $getSession['status'],
                                    'minprice' =>  $getSession['minprice'],
                                    'maxprice'  => $getSession['maxprice'],
                                    'ptype'     => $getSession['ptype'],
                                    'select_menu'=>$getSession['select_menu'],
                                );


            }
    else{
            // echo 'property else';exit;
                $formData = array(
                                    'city'     =>  0,
                                    'area'     =>  0,
                                    'status'  =>  0,
                                    'minprice' =>  0,
                                    'maxprice'  =>  0,
                                    'ptype'     =>0,
                                    'select_menu'=>'home',
                                );
                $results = Property::paginate(6);
    }    

    return view('search', array('page' => 'search','formData'=>$formData,'results'=>$results));
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire