Geoff_S
Geoff_S

Reputation: 5107

Eloquent, multiple where clause with newest record

In laravel, I have a simple call that hits the userRecord table with a given email and grabs the first one.

This works but I want to check the created_at column and valid column as well to get the newest one that's valid (newest created_at date where today is before the valid date)

Existing line:

$get= (userRecord::where('email', $request->query->get('email')->where(curdate() < 'valid'))->first());

So rather than just getting the first, I want to add those column conditions. But I can't quite reconcile how to use the multiple where conditions and replace the first() call with just the newest

Upvotes: 1

Views: 43

Answers (1)

Alberto Guilherme
Alberto Guilherme

Reputation: 348

In laravel you can make multiple wheres like this:

->where([['first_where'],['second_where'],['n_where']])

About the newest:

You can order by the created_at and use desc (bigger to small) like this:

->orderBy('created_at','desc') And then, you can use the first method

Upvotes: 1

Related Questions