Sebastian De Deyne

Tim MacDonald on HasOne relationships in Laravel ∙ #laravel #eloquent

I’ve used HasOne relationships for 1:1 relationships, but those are rare. I haven’t considered using them to scope down relationships, like having one default payment method in a set of n methods.


class User extends Model
    public function paymentMethods(): HasMany
        return $this->hasMany(PaymentMethod::class);

    public function defaultPaymentMethod(): ?HasOne
        return $this->hasOne(PaymentMethod::class)


After reading Tim’s post, I have a feeling there are some places where I needed this but didn’t think of it at the time…

More Laravel & Eloquent

Vite with Laravel: Using Inertia.js

Vite with Laravel: Using TypeScript

Vite with Laravel: Using React