Laravel Artisan Generator Command: The make:request Command


John Koster

Published on December 7, 2016

The make:request command can be used to generate new request validation request classes. The command accepts a name for the newly created request class. The name will be used as the name of the class and the file.

The following example demonstrates how to call the make:request command:

1# Generate a new form request validation request class.
2php artisan make:request StoreDrinksPostRequest

A new app/Http/Requests/StoreDrinksPostRequest.php file would be created and contain contents similar to the following:

3namespace App\Http\Requests;
5use App\Http\Requests\Request;
7class StoreDrinksPostRequest extends Request
9 /**
10 * Determine if the user is authorized to make this request.
11 *
12 * @return bool
13 */
14 public function authorize()
15 {
16 return false;
17 }
19 /**
20 * Get the validation rules that apply to the request.
21 *
22 * @return array
23 */
24 public function rules()
25 {
26 return [
27 //
28 ];
29 }

This command will not overwrite any existing form request validation request classes that have the same name. An error stating Request already exists! will be displayed instead.

The make:request command is also capable of generated namespaced classes. Accomplish this by separating namespace sections using the \ when supplying the request name. A nested directory will be created and the correct namespace will be set on the generated class.

Explore The Blog