user5405648
user5405648

Reputation:

Laravel: where not expired?

I have a column called expires_at and its a datetime in my MySQL database. I need to fetch records where expires_at hasn't been met, how can I do this?

My current query looks like this Model::where('user_id', Auth::user()->id)->get();

Upvotes: 1

Views: 2047

Answers (2)

Sheetal Mehra
Sheetal Mehra

Reputation: 518

You can also try this.

use Carbon\Carbon;
use App\Model;

$now = Carbon::now();
Model::where('user_id', Auth::user()->id)
     ->where('expired_at', '>', $now)
     ->get();

Upvotes: 3

abr
abr

Reputation: 2129

https://laravel.com/docs/5.6/queries#where-clauses

If you're looking to compare with current date, try:

Model::where('user_id', Auth::user()->id)
     ->whereDate('expired_at', '>', date('Y-m-d H:i:s'))
     ->get();

Upvotes: 1

Related Questions