We know laravel provide several in-built laravel validation like required_if, required_without etc. If you need to add validation rules like required if other field is empty in laravel then you can do it using required_if or required_without.
I am going to explain you, If you give profile image uploading in your user profile page then if already added profile image then you do not need to upload every time image, So at that time you can add validation required_if or required_without.
So, you can use this way:
'image' => 'required_if:image_old,'
--OR--
'image' => 'required_without:image_old,'
You can see below validation method example and use this way as i added method example:
required_if:
public function googleFormPost(Request $request)
{
$request->validate([
'name' => 'required',
'image' => 'required_if:image_old,'
]);
dd("Done!");
}
required_without:
public function googleFormPost(Request $request)
{
$request->validate([
'name' => 'required',
'image' => 'required_without:image_old,'
]);
dd("Done!");
}
I hope you found your best....
Do you like below Tutorials ?
- Laravel 5.6 - Collection could not be converted to int
- Laravel - How to generate secure https URL from route?
- Laravel - Vue JS File Upload Example
- How to get last 7 days data in Laravel?
- Laravel Validation required if other field empty example
- Laravel Eloquent - When Case Statement in Select Query Example
- Laravel 7.x and 6.x Passing Variable to Javascript Example
- How to pass PHP variables in JavaScript or jQuery?