1.

Explain validation in Laravel.

Answer»

Validation in programming is necessary to make sure the system is receiving the expected data. Like other frameworks, LARAVEL also have validations.

ValidatesRequests is the trait which is been used by Laravel classes for validating input data. For storing data we generally use create or store methods that we can define at Laravel routes along with a get method.

Laravel Validate method is available in Illuminate\Http\Request object. If validation rule passes, your code will execute PROPERLY. If it fails then an exception will be THROWN and the user will get a proper error response for HTTP requests whereas JSON response will be generated if the request was an AJAX request.  

Basic example of how validation rules are defined will be like :

/** * Store a post. * * @param  Request $request * @return Response */ public function store(Request $request) {    $validatedData = $request->validate([        'title' => 'required|UNIQUE:posts|max:255',        'BODY' => 'required',    ]); }

Title and body will be a required field. Validation rule execution will be sequential so if unique fails then 255 characters validation will not be executed.



Discussion

No Comment Found