Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

noblemfd's avatar

How to format date in Unique Rules

In my Laravel5.8 project I have this Unique Rules code:

        '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('start_date', $this->start_date);
        })               
        ],

How do I format my start_date in the code to:

            'date',
            'date_format:Y-m-d',

Thank you

0 likes
1 reply
tisuchi's avatar
tisuchi
Best Answer
Level 70

@noblemfd

Have you thought this way to implement?

Carbon\Carbon::parse($this->start_date)->format('Y-m-d')
'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('start_date', Carbon\Carbon::parse($this->start_date)->format('Y-m-d'));
	})               
],
6 likes

Please or to participate in this conversation.