Laravel passes value to method along route
I have:
Route::delete('admin/sanitise/{id}/delete', ['as' => 'admin.sanitise.delete', 'uses' => 'ProductController@delete']);
FROM
public function delete($id, $hard = false) {
$product= Product::find($id);
if($hard) {
$product->destroy();
} else {
$product->delete();
}
}
This allows both admins and admins to uninstall products, but I want admins to be able to uninstall them.
The specified route is available only to administrators.
What do I need to set the route to $ hard to true?
+3
source to share
1 answer
You would do something like this:
Route::delete('admin/sanitise/{id}/delete/{hard?}', ['as' => 'admin.sanitise.delete', 'uses' => 'ProductController@delete']);
The question icon in {hard?}
tells laravel that this is an optional route parameter.
When you use this route somewhere, you must set the params array as
$url = route('admin.sanitise.delete', ['id' => $someId, 'hard' => true]);
If you don't set the hard key in the route parameters, the default value false
from your function definition will be used .
+2
source to share