AntonKad AntonKad - 7 months ago 22
PHP Question

Laravel - Parameters are not forward by the route

Route

Route::get('restaurants/@{latitude},{longitude},{radius}', 'RestaurantsController@nearbyRestaurants');


I tried doing my own request at first, then did directly a validator in the controller

Controller's methode

public function nearbyRestaurants(Request $request)
{
$validator = \Validator::make($request->all(), [
'latitude' => 'digits_between:-90,90',
'longitude' => 'digits_between:-180,180',
'radius' => 'numeric'
]);

dd($validator);
}


Results of dd(valisator)

#initialRules: array:3 [▼
"latitude" => "digits_between:-90,90"
"longitude" => "digits_between:-180,180"
"radius" => "numeric"
]
#rules: array:3 [▼
"latitude" => array:1 [▼
0 => "digits_between:-90,90"
]
"longitude" => array:1 [▼
0 => "digits_between:-180,180"
]
"radius" => array:1 [▼
0 => "numeric"
]
]


Solution

Instead of :

public function nearbyRestaurants(Request $request)
{

}


Use

public function nearbyRestaurants($latitude, $longitude, $radius)
{

}


Using dd($validator); i can now see the data:

#data: array:3 [▼
0 => "59.93141200"
1 => "30.31992300"
2 => "15"
]

Answer

What you're getting from the URL are arguments for the controller, not arguments from the $_GET and $_POST global, which is what the validator needs. What you want is this:

public function nearbyRestaurants(Request $request, $latitude, $longitude, $radius)

Then, manually create a validator and pass those in as items.