У нас есть страница ресторана, где пользователь может добавить свой почтовый индекс, и мы показываем рестораны. Мы решили это так: 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);
});
Итак, теперь мы хотели бы иметь только для каждого zip, который является db, такую страницу, как: test.com/restaurants/zip
Есть ли у кого-нибудь предложения?






Не уверен, понял ли я ваш вопрос. Но мне кажется, что вы просто хотите передать почтовый индекс как параметр url, а не в запросе GET.
Если это правда, вы можете просто получить zip как второй параметр для метода showAllRestaurants () следующим образом:
public function showAllRestaurants(Request $request, $zip_code){
//...
}
Теперь zip_code получен в переменной $ zip_code внутри вашего метода.
И измените web.php для поддержки этого.
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');
Чтобы избежать конфликтов в этом случае маршрутизации, вы должны использовать какое-нибудь выражение регулярного выражения, чтобы сообщить laravel, что такое zip_code, иначе, если вы скажете / Restaurants / search, он будет думать, что слово «search» - это zip_code.
В случае, если ваш zip_code содержит только числа. Вы можете добавить предложение where () к маршрутам, как показано ниже.
Route::get('/{zip_code}', 'RestaurantController@showAllRestaurants')->where('zip_code', '[0-9]+');
Route::post('/{zip_code}', 'RestaurantController@showAllRestaurants')->where('zip_code', '[0-9]+');
Если ваш zip_code содержит другие символы, вам следует поискать в Google (или создать его самостоятельно) какое-нибудь регулярное выражение, соответствующее вашему формату zip_code.
Надеюсь, ты этого хочешь.
Спасибо, Эдуардо, мы решили проблему с вашей помощью.
Comprendo? Неа