Adnan
Adnan

Reputation: 22

Laravel between dates

I want to check the validity of promo code by comparing date in eloquent but dont know why its not working

$promoCode = self::where('code', $code)
        ->where('start_date', '>=', Carbon::now()->toDateString())
        ->where('end_date', '<=', Carbon::now()->toDateString())
        ->first();

If i comment out 2nd and 3rd line then it works fine. MySQL column type is date. I also tried whereDate method and thats not working too

Upvotes: 0

Views: 75

Answers (2)

Derrick Noutais
Derrick Noutais

Reputation: 11

Shouldn't the start_date be before now. If so now should be between. Try:

$promoCode = self::where('code', $code)
    ->where('start_date', '<=', Carbon::now()->toDateString())
    ->where('end_date', '<=', Carbon::now()->toDateString())
    ->first();

Upvotes: 1

Jack Wire
Jack Wire

Reputation: 721

It looks like you inverted the signs. You are looking for something that is older than start_date and younger than end_date.

Upvotes: 1

Related Questions