Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
6 / 7
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CustomerAddress
85.71% covered (warning)
85.71%
6 / 7
50.00% covered (danger)
50.00%
1 / 2
3.03
0.00% covered (danger)
0.00%
0 / 1
 customer
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 booted
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace App\Models;
4
5use App\Models\admin\Customer;
6use Illuminate\Database\Eloquent\Model;
7
8class CustomerAddress extends Model
9{
10    protected $fillable = ['customer_id', 'label', 'name', 'phone', 'address_line', 'city', 'is_default'];
11
12    protected $casts = ['is_default' => 'boolean'];
13
14    public function customer()
15    {
16        return $this->belongsTo(Customer::class, 'customer_id');
17    }
18
19    protected static function booted(): void
20    {
21        // exactly one default address per customer
22        static::saved(function (self $address) {
23            if ($address->is_default) {
24                static::where('customer_id', $address->customer_id)
25                    ->where('id', '!=', $address->id)
26                    ->update(['is_default' => false]);
27            }
28        });
29    }
30}