Home > Blockchain >  Validation check if requested value exists in another table Laravel 9
Validation check if requested value exists in another table Laravel 9

Time:02-03

Hello i am trying to make a validation that checks the inserted values if they exist or not in other tables. This is the code i have done now:

$request->validate([
            'order_number' => 'required',
            'client_id' => 'required,exists:clients',
            'description' => 'required',
        ]);

These values are inserted in a table called order but the client_id is taken from another table called clients and i want to verify if the value of client_id exists in the row id of the table clients

CodePudding user response:

Please check here Laravel Validation

As mentioned in laravel document you need to specify a column name, because if you don't, it will use the key in the request for the column name, so in your example, it will be the clients table and client_id column but you need to specify the column name in exists rule, it will be something like this:

$request->validate([
            'order_number' => 'required',
            'client_id' => 'required|exists:clients,id',
            'description' => 'required',
        ]);

Also, you used the wrong syntax for separating rules, after required you need to use a pipe(|) to separate your rules and use multiple validations.

  • Related