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

cj_wotbot's avatar

Unique validation that covers three tables

Firstly, this is the relationships of my tables

//Organization Class
public function resellers()
    {
        return $this->hasMany(Reseller::class);
    }

//Reseller Class
public function organization()
    {
        return $this->belongsTo(Organization ::class);
    }

public function addresses()
   {
    return $this->hasMany(ResellerAddress::class, 'reseller_id');
   }

 //ResellerAddress::class
public function reseller()
    {
        return $this->belongsTo(Reseller::class, 'reseller_id');
    }

How do you implement unique validation to a field that belongs to ResellerAddress but considers that it is unique only to the Organization?

0 likes
2 replies
getupkid's avatar

Can you clarify your question?

Are you asking for a validation rule that checks to make sure that each "Reseller Address" is unique to an "Organization"?

cj_wotbot's avatar

@getupkid Yeah, that's basically the question.

This is my validation rule if it's only two tables organization hasMany resellers or reseller hasMany resellerAddresses

$this->rules['name'] = [
                'required',
                Rule::unique('resellers', 'name')
                    ->ignore($request->get('id'))
                    ->where('organization_id', $someOrgId)
                ];
 $this->validate($request, $this->rules, $this->messages);

I have a property called rules that is an array for the validation rules.

Please or to participate in this conversation.