Gatzmar
Gatzmar

Reputation: 143

Get the latest record of a particular id in a table

    ScholarCard
    scholar_card_id         grade       scholar_id
    1                       grade-1         1
    2                       grade-1         2
    3                       grade-2         1
    4                       grade-2         2
    5                       grade-3         1

$scholar_cards = ScholarCard::where('scholar_id','=',$scholar_id)->orderBy('scholar_card_id','DESC')->get();

I have a table for a card of grades and i want to display the latest created card. Like for example for scholar_id 1,there are three 1 on the column.How do you display only the scholar_id 1 with the latest or last creation.This table has only few record for simplicity.

Upvotes: 1

Views: 52

Answers (1)

Laerte
Laerte

Reputation: 7083

You could use first() function. Try this:

$scholar_card = ScholarCard::where('scholar_id','=',$scholar_id)
                           ->orderBy('scholar_card_id','DESC')
                           ->first();

Upvotes: 1

Related Questions