В Laravel -5.8 проекте веб-приложения я пытаюсь проверить поля ввода Dynami c, используя Правила запросов.
У меня есть две эти модели
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 is класс основной модели, в то время как AppraisalGoalDetail - это массив полей, который сохраняется на основе appraisal_goal_id, который является внешним ключом, полученным из id в AppraisalGoal.
Обратите внимание, что это один ко многим: один AppraisalGoal для многих AppraisalGoalDetail. Это указано в Controller
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',
];
}
}
Controller
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);
})
],
Как мне добиться этого с помощью правил запроса?
Спасибо.