Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

smart way to handle request

The Smart Way To Handle Request Validation In Laravel 5.8 (Laravel form validation)

In this tutorial I will explain a complete guide of Laravel form validation, As many of you already know there are many ways to validate requests in Laravel, Handling request validation is very crucial part of any application. Laravel has some great feature which deals with this very well.

Step 1: Make a http request for validate your data:

Run this command :

php artisan make:request StudentValRequest

Now you can see the file app/Http/Requests:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StudentValRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'student_name' => 'required',
            'student_email' => 'required',
            'student_address' => 'required',
            
        ];
    }
}

Step 2: Now include your Http request in your controller:

use App\Http\Requests\StudentValRequest;

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\StudentValRequest;
use PDF;
use Session;
use App\ExportPdfs;

class ExportPdfController extends Controller
{

	 public function store(StudentValRequest $request)
	  {
        $data = new ExportPdfs();
        $data->student_name    = $request->student_name;
        $data->student_email   = $request->student_email;
        $data->student_address = $request->student_address;
        $data->save();
        return redirect()->route('pdf.list')->with('flash_message_success','Record added Successfully');
       
	}
}

A brief introduction about laravel validation : click here

Output : in this tutorial, we learned a complete laravel form validation

Read also : How to quickly generate data using faker and tinker ?

laravel-form-validation
laravel-form-validation