Method 1

Creating request class

For more complex validation scenarios, you may wish to create a "form request". Form requests are custom request classes that contain validation logic. To create a form request class, use the make:request Artisan CLI command

php artisan make:request RenterRequest
class RenterRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true; // this should be true must
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(Request $request)
    {
        return [
            'name' => 'required',
            'phonenumber' => 'required|numeric|min:11',
            'NID' => 'required',
            'hometown' => 'required',
        ];
    }
}

So, how are the validation rules evaluated? All you need to do is type-hint the request on your controller method. The incoming form request is validated before the controller method is called, meaning you do not need to clutter your controller with any validation logic:


public function store(RenterRequest $request)
{
Renter::create([
'name' => $request->name,
'phonenumber' => $request->phonenumber,
'NID' => $request->NID,
'hometown' => $request->hometown,
]);
return redirect()->route('renters.index');
}

Method 2



results matching ""

    No results matching ""