Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

latz's avatar
Level 3

Validate unique value (two fields)

I would like to check two fields. The two fields should be unique together. In the table "training" there are two columns: training_Id and team_Id.

$this->validate($request, [
 'trainingday' => 'required|date|unique:training,trainingID', ????

How can I validate the two fields together?

0 likes
4 replies
latz's avatar
Level 3

I have been dealing with this problem for a while. This does not really work:

...
  'trainingdate' => 'required|date|unique:training,trainingdate,NULL, id,"teamId",'.$request("teamId"),
...

But if the TeamID is missing in the postrequest, a 500 error occurs. There should be a message that the TeamId is missing? What am I doing wrong?

latz's avatar
Level 3

@nakov Do you have any idea how I could solve this? If the teamID is not set in the request, an error occurs. (500 - "message": "Function name must be a string"). A required-rule should take effect during validation.

Nakov's avatar

500 is a server error, might be because you are missing a value and the value after the last , is empty or I don't know what, you have storage/logs in each laravel project where you can find what the full error trace is. Maybe because you are using $request() which is not a function.. it should be without $ to use the helper function.

Now back to your validation, have you tried using the fluent API that I shared from the documentation above?

'trainingdate' => ['required', 'date', \Illuminate\Validation\Rule::unique('training')
            ->where(function($query) {
                $query
                    ->whereNull('trainingdate')
                    ->where('teamId', request('teamId'));
})]
1 like

Please or to participate in this conversation.