Tim MacDonald on HasOne relationships in Laravel

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.

<?php
 
class User extends Model
{
public function paymentMethods(): HasMany
{
return $this->hasMany(PaymentMethod::class);
}
 
public function defaultPaymentMethod(): ?HasOne
{
return $this->hasOne(PaymentMethod::class)
->whereDefault();
}
}
 
$user->defaultPaymentMethod;

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…

Vite with Laravel

I've had an eye on Vite for a while. With a stable release out the door (2.0, as 1.0 never left the release candidate stage) it seemed like a good time to give it a shot.

Vite is a frontend build tool like webpack. Instead of bundling development assets, Vite serves native ES modules transpiled with esbuild from the dev server. This means there's a lot less bundling to do, and results in a very fast developer experience. For production builds, Vite uses Rollup to bundle the assets.

Read more