Laravel - How to validate unique department name to company_id

Viewed 107

In my Laravel-8 Job Portal application, I have these four (4) tables (models):

Department, User, Company and CompanyProfile

A user can only appear once on the user table with unique email. But he can have more than one company profile, since he can work remotely for more than one company:

These are the models:

class Department extends Model
{
    protected $table = 'departments';
    protected $primaryKey = 'id';
    protected $fillable = [
                'id',
                'company_id',
                'name',
            ];

    public function company()
    {
        return $this->belongsTo('App\Models\Company','company_id');
    }
}

class Company extends Model
{    
     protected $table = 'companies';
     protected $primaryKey = 'id';
     protected $fillable = [
                'id',
                'name',
                'org_image',
             ];

    public function users()
    {
        return $this->hasMany('App\Models\User');
    }

    public function departments()
    {
     return $this->hasMany('App\Models\Department');
    }
}

class User extends Authenticatable
{
    protected $hidden = [
        'password',
        'remember_token',
    ];
    
    protected $fillable = [
        'name', 
        'first_name',
        'other_name',
        'last_name',
        'username',
        'email', 
        'password', 
    ];

    public function profile(){
        return $this->hasMany('App\Models\CompanyProfile', 'employee_id');
    }

    public function company(){
        return $this->hasMany('App\Models\CompanyProfile', 'company_id');
    }
}

class CompanyProfile extends Model
{    
     protected $table = 'company_profiles';
     protected $primaryKey = 'id';
     protected $fillable = [
                'id',
                'user_id',
                'company_id',
                'department_id',
                'employment_date',
            ];
}

I have this Request rules validation for the Department:

public function rules()
{
    return [
        'name' => [
            'required',
            'string',
            'min:2',
            'max:100',
        ],

        'company_id' => [
            'required',
        ],

    ];
}

How do I validate a department name to be unique with company_id in the Request Rules?

Thanks

1 Answers

You can access company id using $this keyword => $this->company_id, Follow your rule function below,

use Illuminate\Validation\Rule;

public function rules()
{
    return [
            'name' =>  [
                         'required',
                         'string',
                         'min:2',
                         'max:100',
                          Rule::unique('departments')
                                ->where('company_id', $this->company_id)
                        ]
        ];
}

public function messages()
{
    return [
        'name.unique' => 'Department name and company_id has to be unique.',
    ];
}
Related