It's posible to validate request with rules for additional fields, or remove that fields from request?
Simple example, I have FormRequest object with rules:
public function rules() {
return [
'id' => 'required|integer',
'company_name' => 'required|max:255',
];
}
And when I get post request with any other fields I want to get error/exception in controller or I want to get only id and company_name fields, with no any others. It's exist any feature in laravel with that, or I must make it in my way?
Your post request with laravel has more stuff than just your form input so you need to check Request to see what is inside.
To get only the fields you want you can use:
$request->only(['fieldname1', 'fieldname2', 'fieldname3']);
or
$request->except(['fieldnameYouDontWant1', 'fieldnameYouDontWant2', 'fieldnameYouDontWant3']);
to exclude the ones you don't want.
To remove key from request in Laravel, use:
$request->request->remove('key')
Since Laravel 5.5 you can do this in your controller:
public function store(StoreCompanyRequest $request)
{
Company::create($request->validated());
}
The validated() function returns only the fields that you have validated and removes everything else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With