Sometimes we need to validate dependency fields like price depends on currency and currency depends on price. Assume if the user gives input to price then he must fill up currency input. Otherwise, both field is nullable. How can we solve this validation problem in Laravel? We can solve this validation problem using required_with helper.

In this tutorial, I will show you how to use required_with to validate the laravel required if another field is empty types of validation in the Laravel application.

Let's see the example of laravel validation required if another field is present:

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function __invoke(Request $request)
    {
        $request->validate([
           'price'=>'required_with:currency|numeric',
           'currency'=>'required_with:price',
        ]);
    }
}

 

Read also: Laravel Associative And Nested Array Validation Example

 

Conclusion

Now we know laravel required if another field is empty. Now we know how to validate laravel validation required if another field value. Hope this required if other field is null laravel tutorial will help you.