Contact

Blogs

Take a look at the things that interest us.

Laravel Requests

Wesley Wesley 3 years

Laravel Request

Today I'm going to learn about the laravel request classes. laravel request classes are handy for when you are creating a post or update form. It's also easy to use, and this way you can use one validation request in multiple laravel classes with the same data.

Docs

To create a form request class, you may use the make:request artisan command.

php artisan make:request StorePostRequest

The generated form request class will be placed in the app/Http/Requests directory. If this directory does not exist, it will be created when you run the make:request command.

Example

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ];
}

To use the request in your controller, all you'll need to do is include to class

use App\Http\Requests\StorePostRequest

/**
 * Store a new blog post.
 *
 * @param  \App\Http\Requests\StorePostRequest  $request
 * @return Illuminate\Http\Response
 */
public function store(StorePostRequest $request)
{
    // The incoming request is valid...

    // Retrieve the validated input data...
    $validated = $request->validated();
}

If validation fails, a redirect response will be generated to send the user back to their previous location. The errors will also be flashed to the session so they are available for display.

Laravel Requests 2021-08-24 08:52:07

There are no comments.

4569

Have questions about our services?

Contact our Sales team to get answers.

Contact Us
gomibako@aska-ltd.jp