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.
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()
]);
}
@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(???)
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