How to pass id to form request in laravel?

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

How to pass id to form request in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by ryleigh , 10 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.

Related Threads:

How to pass a parameter from request in laravel?
How to pass variable from guzzle request in laravel?
How to redirect with id in laravel?
How to return ajax data request by id in octobercms?
How to pass unique id in graphql query?
How to get parent form id using knockout.js?