Handle form data smartly with FormRequest class of Laravel

/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return false; // trough unauthorized error
return true; // no check, allowing to go further return auth()->check(); // returns true if user is logged in /* your custom logic to check user's role. If user has
the 'customer' role it will return true or false. */
return auth()->user()->hasRole('customer');
}

rules()

/**
* Get the validation rules that apply to the request.
*
* @
return array
*/
public function rules()
{
return [
'name' => 'required|max:100',
'email' => 'required|email|unique:users',
'password' => 'required|confirmed',
'contact_number' => 'required',
'shipping_address' => 'required',
'coupon' => 'nullable|exists:coupons,code',
'products.*.id' => 'required|exists:products,id',
'products.*.price' => 'required|numeric',
'products.*.quantity' => 'required|numeric'
];
}

messages()

/**
* @return array|string[]
*/
public function messages()
{
return [
'product_id.required' => 'The product field is required',
'product_id.exists' => 'The product do not exist',

];
}

attributes()

// before using attributes method"errors": {
"product_id": [
"The product id field is required."
]
}
// after using attributes methodpublic function attributes()
{
return [
'product_id' => 'product',
];
}
"errors": {
"product_id": [
"The product field is required."
]
}

merge($input)

dump($request->all());
/*
[
'name' => 'John Doe',
'email' => 'john@example.com'
]
*/

// merging is_active key with the request
$request->merge([
'is_active' => true
]);

dump($request->all());
/*
[
'name' => 'John Doe',
'email' => 'john@example.com',
'is_active => true
]
*/

prepareForValidation()

protected function prepareForValidation()
{
$this->merge([
'date' => now()->toDateString(),
'user_id' => auth()->id()
]);
}

all()

public function store(App\Http\Request\UserRequest $request)
{
echo $request->has('name'); // true
}
// and in the UserRequest class you overridden the all() method
public function all($keys = null)
{
return [
'foo' => 'bar'
];
}
// after overriding the all() method
public function store(App\Http\Request\UserRequest $request)
{
echo $request->has('name'); // false
echo $request->has('foo'); // true
}
public function all($keys = null)
{
// getting actual request array
$payload = parent::all();

// adding new value
$payload['foo'] = 'bar';

// returning the with the new value
return $payload;
}

failedAuthorization()

protected function failedAuthorization()
{
throw new AuthorizationException("Sorry you don't have access");
}

--

--

--

A software engineer from Bangladesh.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The inclusive nature of Serverless

Pointers to Derived Classes

Monitoring Your Kubernetes Ingress with Ambassador, Prometheus, and Grafana

Easy and Automatic (CI/CD) Deployment Solution for Node.js

Python for kids

What is Brython?

python language

Orion: A next-generation automation testing tool

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mehedi Hassan Sunny

Mehedi Hassan Sunny

A software engineer from Bangladesh.

More from Medium

What is Laravel? What are the Main Features?

Converting Your Test Suite to PEST

JWT Authentication (Laravel Part )

Why I built Waterfall, an open-source package to perform cascading deletes.