Mathieu Mourareau
Mathieu Mourareau

Reputation: 1220

Query with date comparaison

I try to get the last recorded query from a table

the table could have two differents records but with the same id "licencie_id" , i would like to get only one record who find the latest record of licencie_id with a date "dt_demande" comparaison for exemple because i have a field 'dt_demande' .

here my two records in the table :

id :46
licencie_id :36
dt_demande : 2017/03/23

id:50
licencie_id :36
dt_demande :2017/04/06

i would like to get id:50 because it's the lastest record from this licence.

here my actualy query who returns my the two records :

$demande = DemandeChangementClub::where('licencie_id' , $licencie->id)->where('dt_demande' , '<' , Carbon::now())->last();

someone have an idea to get the latest record only ? thanks a lot in advance

Upvotes: 0

Views: 39

Answers (1)

Amr Aly
Amr Aly

Reputation: 3905

We can do something like this:

$demande = DemandeChangementClub::where('licencie_id' , $licencie->id) 

->orderBy('dt_demande', 'desc')->first();

or you can :

 $demande = DemandeChangementClub::where('licencie_id' , $licencie->id) 

 ->latest('dt_demande')->first();

Upvotes: 3

Related Questions