In this situation you can write your custom validation rule. For example, let create a class called CustomValidator (place him in folder like 'App\Services' or another that you want).
CustomValidator.php
namespace App\Services;
class CustomValidator {
public function myexistsValidate($attribute, $value, $parameters, $validator) {
for ($i = 0; $i < count($parameters); $i += 2) {
$count = \DB::table($parameters[$i])->where($parameters[$i + 1], $value)->count();
if (!$count) {
return false;
}
}
return true;
}
}
We created a new rule called myexists
. This rule can accept pair of paramaters divided by commas lile this: 'myexists:table1,searchfield1,table2,searchfield2...'`
I wrote very simple example of implementation of this rule, ofc you can add some approach or another validations...
Next you must register your own validation rule in AppServiceProvider in method boot
(string that you place as a first parameter will be a name of new rule):
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use \Validator;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Validator::extend('myexists', 'App\Services\CustomValidator@myexistsValidate');
}
public function register()
{
}
}
Next in your FormRequest place code like this:
public function rules()
{
$rules = [
'id' => 'myexists:tableName1,field1,tableName2,field2',
];
return $rules;
}
You can add a validation message for this rule, for example in your lang\en\validation.php
file 'myexists' => 'Field :attribute must exists in all described tables.'