Laravel-如何编写外键数组中唯一字段的验证组合

时间:2020-03-25 11:46:14

标签: laravel

在Laravel-5.8 Web应用程序项目中,我试图使用请求规则来验证动态输入字段。

我有这两个模型

class AppraisalGoal extends Model
{
  public $timestamps = false;

  protected $table = 'appraisal_goals';

  protected $fillable = [
              'id',
              'goal_type_id',
              'parent_id',
              'appraisal_identity_id',
              'employee_id',
              'company_id',
              'weighted_score',
              'goal_title',
          ];

  protected $dates = [];

  protected $casts = [];

  public function goaltype()
  {
    return $this->belongsTo('App\Models\Appraisal\AppraisalGoalType','goal_type_id');
  }

  public function employee()
  {
    return $this->belongsTo('App\Models\Hr\HrEmployee','employee_id');
  }

  public function appraisalgoaldetail(){
    return $this->hasMany('App\Models\Appraisal\AppraisalGoalDetail');
  }

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

class AppraisalGoalDetail extends Model
{
 public $timestamps = false;
  protected $table = 'appraisal_goal_details';
  protected $primaryKey = 'id';

  protected $fillable = [
              'name',
              'company_id',
              'appraisal_goal_id',
              'kpi_description',
              'activity',
              'start_date',
              'end_date',
              'appraisal_identity_id',
              'employee_id',
          ];

  protected $dates = [
    'start_date',
    'end_date'
    ];

protected $casts = [
 'data' => 'array',
];

public function appraisalgoal()
    {
    return $this->belongsTo('App\Models\Appraisal\AppraisalGoal');
  }

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

AppraisalGoal是主要的模型类,而AppraisalGoalDetail是一个基于基于appraisal_goal_id(是从AppraisalGoal中的id派生的外键)保存的字段数组。

请注意,它是一对多的:从一个到多个AppraisalGoalDetail。这在控制器中说明

class StoreAppraisalGoalRequest extends FormRequest
{
  public function rules()
  {
    return [
        'goal_type_id' => [
            'required', 
            Rule::unique('appraisal_goals')->where(function ($query) {
           return $query->where('appraisal_identity_id', $this->appraisal_identity_id)
                ->where('goal_type_id', $this->goal_type_id)
              ->where('employee_id', $this->employee_id);
        })               
        ],
        'goal_title' => [
             'required', 
             'string',
             'min:5',
             'max:100',                 
            Rule::unique('appraisal_goals')->where(function ($query) {
           return $query->where('appraisal_identity_id', $this->appraisal_identity_id)
                ->where('goal_title', $this->goal_title)
              ->where('employee_id', $this->employee_id);
        })                
        ],


        'kpi_description'           => 'required|array',
        'kpi_description.*'         => 'required',

        'activity'           => 'required|array',
        'activity.*'         => 'required',                     

    ];
  } 
}

控制器

public function store(StoreAppraisalGoalRequest $request)
{            
    $userCompany = Auth::user()->company_id;
    $employeeId = Auth::user()->employee_id;
      $identities = DB::table('appraisal_identity')->select('id','appraisal_name')->where('company_id', $userCompany)->where('is_current', 1)->first();
      $employees = DB::table('hr_employees')->select('id')->where('id', $employeeId)->first();

  DB::beginTransaction(); 
    try {
        $goal = new AppraisalGoal();
        $goal->goal_type_id             = $request->goal_type_id;
        $goal->appraisal_identity_id    = $request->appraisal_identity_id;
        $goal->employee_id              = $request->employee_id;  //$employeeId;   //$request->employees_id
        $goal->weighted_score           = $request->weighted_score;
        $goal->goal_title               = $request->goal_title;
        $goal->goal_description         = $request->goal_description;
        $goal->company_id               = Auth::user()->company_id;

       $goal->save();        

        foreach ( $request->activity as $key => $activity){

            $goaldetail = new AppraisalGoalDetail();

             $goaldetail->kpi_description            = $request->kpi_description[$key];
             $goaldetail->activity                   = $request->activity[$key];                 
             $goaldetail->appraisal_goal_id          = $goal->id;
             $goaldetail->appraisal_identity_id      = $goal->appraisal_identity_id;
             $goaldetail->employee_id                = $goal->employee_id;
             $goaldetail->save();
         }

     DB::commit(); 

            Session::flash('success', 'Appraisal Goal is created successfully');
            return redirect()->route('appraisal.appraisal_goals.index');
    } catch (Exception $exception) {
  DB::rollback();
            Session::flash('error', 'Action failed! Please try again');
            return redirect()->route('appraisal.appraisal_goals.index');
    }
}  

AppraisalGoalDetail,活动中的字段与appraisal_goal_id,employee_id和appraisal_identity_id唯一。

同样,kpi_description与appraisal_goal_id,employee_id和appraisal_identity_id唯一

这是我到目前为止所拥有的:

        'kpi_description'           => 'required|array',
        'kpi_description.*'         => 'required',

        'activity'           => 'required|array',
        'activity.*'         => 'required',   

但是由于它是一个数组,所以不知道如何继续。

我想使其与此处的内容相似:

        'goal_type_id' => [ 
            Rule::unique('appraisal_goals')->where(function ($query) {
           return $query->where('appraisal_identity_id', $this->appraisal_identity_id)
                ->where('goal_type_id', $this->goal_type_id)
              ->where('employee_id', $this->employee_id);
        })               
        ],

如何使用“请求规则”实现这一目标?

谢谢。

1 个答案:

答案 0 :(得分:0)

抱歉,我没有评论消息的声誉,所以我在那儿提问。您有一个数组kpi_descriptionactivity并且它们有一个键,并且您想验证它们的键吗? U可以用kpi_description.firstKey.secondKey.thirdKey = ['required']来做,或者创建自定义规则,我更喜欢用工匠命令php artisan make:rule MyRule来做。如何使用它,您可以在文档https://laravel.com/docs/5.8/validation#custom-validation-rules

中看到
相关问题