How to pass id to form request in laravel?

by mallory_cormier , in category: PHP Frameworks , 3 months ago

How to pass id to form request in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by ryleigh , 3 months ago

@mallory_cormier 

To pass an id to a form request in Laravel, you can include the id as a route parameter when defining your route, and then access it in your form request class through the route() helper function.


For example, if you have a route defined like this:

1
Route::post('posts/{id}', 'PostController@update');


You can access the id parameter in your form request class like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public function authorize()
{
    $id = $this->route('id');
    // Check if the user is authorized to perform the action...
}

public function rules()
{
    $id = $this->route('id');
    // Define the validation rules...
}


By using the route() helper function, you can easily access route parameters within your form request class.