The shop gains an addresses table whose customer_id has a unique index. The model whose table holds the foreign key declares belongsTo, the other hasOne: Address::customer() returns $this->belongsTo(Customer::class), and Customer::address() returns $this->hasOne(Address::class)->withDefault(['city' => 'unknown']).
use App\Models\{Address, Customer};
echo Customer::find(1)->address->city, "\n";
$elif = Customer::find(5)->address;
echo $elif->city, ', exists: ', var_export($elif->exists, true), "\n";
echo Address::firstWhere('city', 'Austin')->customer->name, "\n";Output
Sao Paulo unknown, exists: false Ben Carter
Elif has no row, so withDefault() supplied an unsaved Address, the null-object pattern, where a bare hasOne would return null. Only the unique index keeps it one to one: without it, hasOne quietly returns whichever row MySQL 524 reads first.