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

saqueib's avatar

How to validate date after_or_equal to a column

I have a post and it can have many revisions, revision has an effective_date column, I want to make sure when creating a new revision, effective dates must be >= the previous revision’s effective date.

I have tried using https://laravel.com/docs/5.5/validation#rule-after-or-equal

'effective_date' => 'date|nullable|after_or_equal:effective_date'

but it's not working for me, please help

0 likes
3 replies
BezhanSalleh's avatar

if your using carbon then i don't see the point since you are checking the >= for revision creation. anyhow you could try something as follow:

$post = Post::find(1);
//for post 1 get last revision's effective_date
$last_effective_date = $post->revisions()->select('effective_date')->latest()->first()->created_at;
//check to see if the last effective date for post one is greater than or equal to the current date
if(Carbon::now()->gte($last_effective_date))
{
    //if the condition passes create a new revision for the post 1
    Revision::create([
        'other_fields' => $someValue,
        'effective_date' => Carbon::now()
    ]);
}
saqueib's avatar

@BezhanSalleh thanks for your response, but I was hoping if I can do it using validation rules, I am looking for a unique revision by effective_date greater or equals to posted fields $request->get('effective_date') input.

maybe with additional where clause on Rule:unique('revisions')->where(???)

Thanks anyway

BezhanSalleh's avatar

if your condition is gte than unique is out of the question unless you combine some other column with it. won't this solve your problem though date|nullable|unique:revisions,effective_date|after:effective_date

Please or to participate in this conversation.