Has One of Many

Has One of Many and Scoped Relationships

One-of-many methods pick one row out of many: latestOrder() returns hasOne(Order::class)->latestOfMany('ordered_at'), firstPaidOrder() returns hasOne(Order::class) ->ofMany(['ordered_at' => 'min'], fn ($q) => $q->whereIn('status', ['paid', 'shipped'])), and one() converts a hasMany: bestReview() is $this->reviews()->one()->ofMany('rating', 'max'). A scoped relationship narrows a hasMany: paidOrders() is $this->orders()->withAttributes(['status' => 'paid']).

Picking one order or review, and creating through a scopeSQL
use App\Models\{Customer, Product};
$chen = Customer::find(3);
echo 'latest: #', $chen->latestOrder->id, ' ', $chen->latestOrder->status, "\n";
echo 'first paid: #', $chen->firstPaidOrder->id, "\n";
$php = Product::find(1);
echo $php->latestReview->rating, ' latest, ', $php->bestReview->rating, " best\n";
$hana = Customer::find(8);
$order = $hana->paidOrders()->create();
echo "#{$order->id} for customer {$order->customer_id}: {$order->status}\n";
Output
latest: #8 pending
first paid: #3
4 latest, 5 best
#10 for customer 8: paid

latestOfMany() does not sort and limit: it joins orders to a derived table of MAX(ordered_at) per customer, wrapped in one taking MAX(id) to break ties, so the same SQL serves many customers when eager-loaded. withAttributes() both adds where status = 'paid' to reads and sets status on created models; asConditions: false keeps only the latter.