Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thedocrow0124's avatar

Eloquent compare only to most recent entry in whereRelation

I have 2 tables. ShipmentLine and ShipmentLineExftyDate, with a one to many relationship. I want to use a where clause to compare only the most recent ShipmentLineExftyDate entry. I can only work out how to compare with all the values and not just the most recent one.

$shipments = ShipmentLine::whereRelation('shipment_line_exfty_dates', 'date', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])
->get();

Thanks in advance!

0 likes
13 replies
vincent15000's avatar

Why don't you just add a where clause on the date of the shipment lines ?

thedocrow0124's avatar

@vincent15000 It is the date on the shipment_line_exfty_dates that I only want to get the most recent. A filter on the shipment_line would only filter when shipment lines are made. Basically I want all the shipment_line records which has the most recent shipment_line_exfty_date within the week. The shipment_line_exfty_dates is essentially a history of all the shipment dates that a line has been given, but the most recent is the most up to date one.

1 like
webrobert's avatar
// you have to make sure this format
// for your column (its the only reason I didnt inline it)
[$from, $to] = [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()] ;

$shipments = ShipmentLine::whereHas('shipment_line_exfty_dates', fn ($q) =>
    $q->whereBetween('date', [$from, $to]) 
)->get();
1 like
webrobert's avatar

whereRelation is for when you want

to query for a relationship's existence with a single, simple where condition

In your case you need the closure. So use whereHas

thedocrow0124's avatar

@webrobert Thanks, I may be missing something, but I don't see where that looks at only the latest shipment_line_exfty_dates for that record?

webrobert's avatar

@thedocrow0124

ShipmentLine -> shipment_line_exfty_dates ->

this query is on the relation 'shipment_line_exfty_dates'

$q->whereBetween('date', [$from, $to]) 

you can add to it if needed.

$q->whereBetween('date', [$from, $to])
  ->where('another', 'thing')
thedocrow0124's avatar

@webrobert Thanks very much, that makes sense now. Not sure if this is because of a separate issue or not, but it doesn't seem to return anything, even though there are 2 dates within this week that should show?

select * from shipment_line_exfty_dates;

id, shipment_line_id, date, created_at, updated_at
'48', '50', '2023-01-30', '2022-10-13 10:55:44', '2022-10-13 10:55:44'
'49', '51', '2023-01-30', '2022-10-13 10:57:40', '2022-10-13 10:57:40'

The model looks correct as it should format it as a date?

    protected $dates = ['date'];

    protected $fillable = [
        'shipment_line_id',
        'date',
    ];
    public function shipment_lines()
    {
        return $this->belongsTo(ShipmentLine::class);
    }
webrobert's avatar

@thedocrow0124

[$from, $to] = [
Carbon::now()->startOfWeek()->toDateString(), 
Carbon::now()->endOfWeek()->toDateString()
] ;
thedocrow0124's avatar

@webrobert Thanks. I understand how that should work, but it still returns nothing. I am guessing this is because it won't compare between when they are strings? Do I need to convert my column to timestamp instead of date?

webrobert's avatar

@thedocrow0124, good question, It should work as a 'date' column.

Do you have a cast for it on the model?

   protected $casts = [
        'date' => 'date',
    ];
thedocrow0124's avatar

@webrobert Thanks, that works! Regarding only looking at the latest, using 'latest()' does not seem to look at the latest but matches to any of them?

$shipments = ShipmentLine::
        with('shipment_line_exfty_dates', 'shipments', 'customer_order_lines.colourways.style_versions.styles.designs')
        ->whereHas('shipment_line_exfty_dates', fn ($q) =>
            $q->latest()->whereBetween('date', [$from, $to])
        )->get();
thedocrow0124's avatar

@webrobert Yes, sorry I couldn't word it very well! I have tried latestOfMany() but it just said

Call to undefined method Illuminate\Database\Eloquent\Builder::latestOfMany()

I will read up on those links shortly and see how far I get.

Please or to participate in this conversation.