0

We have a restaurant page where the user can add his zip and we're showing the restaurants. We have solved this like this: web.php

Route::group(['prefix' => 'restaurants', 'namespace' => 'frontEnd', 'middleware'=>'checkzipcode'], function () {
            Route::get('/', 'RestaurantController@showAllRestaurants');
            Route::post('/', 'RestaurantController@showAllRestaurants');
            Route::get('search','RestaurantController@searchRestaurant');
            Route::post('typefilter','RestaurantController@productTypeFilter');

RestaurantController.php

public function showAllRestaurants(Request $request)
    {
        $getZipCode = session::get('zipcode',$request->zip_code);

        if(!empty($getZipCode))
        {

            if(Auth::check()) {
                $country_code = Auth::user()->country_code;
            } else {
                $country_code = Common::GetIPData()->iso_code;
            }

            // get all restaurant using zipcode
            $all_restaurant = Restaurant::leftJoin('restaurant_delivery_areas','restaurant_delivery_areas.restaurant_id','=','restaurants.id')
                            ->leftJoin('zip_codes','zip_codes.id','=','restaurant_delivery_areas.zip_code_id')
                            ->leftJoin('restaurant_cuisines','restaurant_cuisines.restaurant_id','=','restaurants.id')
                            ->where('restaurants.country_code',$country_code)
                            ->where(function ($q) use($getZipCode) {
                                $q->where('restaurants.zip',$getZipCode)
                                ->orWhere('zip_codes.postal_code',$getZipCode)
                                ->where('restaurant_delivery_areas.is_active','=',1);
                            });

So now we would like to have just for each zip which is db a page like: test.com/restaurants/zip

Does anybody have a suggetion?

1
  • Comprendo? Nope Commented Sep 29, 2018 at 14:31

1 Answer 1

1

No sure if i understood your question. But it seems to me that you just want to pass the zipcode as a url parameter, and not in the GET query.

If that's true, you could just receive the zip as the second parameter for the showAllRestaurants() method like this :

public function showAllRestaurants(Request $request, $zip_code){
    //...
}

Now, the zip_code is received on the $zip_code variable, inside your method.

And change the web.php to support that.

Route::group(['prefix' => 'restaurants', 'namespace' => 'frontEnd', 'middleware'=>'checkzipcode'], function () {
        Route::get('/{zip_code}', 'RestaurantController@showAllRestaurants');
        Route::post('/{zip_code}', 'RestaurantController@showAllRestaurants');
        Route::get('search','RestaurantController@searchRestaurant');
        Route::post('typefilter','RestaurantController@productTypeFilter');

To avoid conflits in this case of routing you should use some regex expression to tell laravel what a zip_code is, otherwise if you say /restaurants/search, it will think that the 'search' word is a zip_code.

In the case your zip_code has just numbers. You could add the where() clause on the routes like the following.

 Route::get('/{zip_code}', 'RestaurantController@showAllRestaurants')->where('zip_code', '[0-9]+');
 Route::post('/{zip_code}', 'RestaurantController@showAllRestaurants')->where('zip_code', '[0-9]+');

If your zip_code contains other characters you should google for (or make one yourself) some regex that fit your zip_code format.

Hope that's what you want.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Eduardo, we have solved the problem with your help.

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.