【发布时间】:2021-08-08 07:53:30
【问题描述】:
在我的 Laravel-8 Job Portal 应用程序中,我有这四 (4) 个表(模型):
部门、用户、公司和公司简介
一个用户只能在用户表上出现一次,并且具有唯一的电子邮件。但他可以拥有多个公司资料,因为他可以为多个公司远程工作:
这些是模型:
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',
];
}
我有这个部门的请求规则验证:
public function rules()
{
return [
'name' => [
'required',
'string',
'min:2',
'max:100',
],
'company_id' => [
'required',
],
];
}
如何验证部门名称在请求规则中是否具有 company_id 的唯一性?
谢谢
【问题讨论】:
标签: laravel