【问题标题】:Laravel - How to validate unique department name to company_idLaravel - 如何验证公司 ID 的唯一部门名称
【发布时间】: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


    【解决方案1】:

    您可以使用 $this 关键字访问公司 id => $this->company_id,按照下面的规则功能,

    使用 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.',
        ];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 2021-07-23
      • 1970-01-01
      • 2018-05-24
      相关资源
      最近更新 更多