Files
2026-02-03 15:31:29 +05:00

74 lines
1.5 KiB
PHP

<?php
namespace App\Models\System\Settings\Location;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class UserAddress extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'last_name',
'first_name',
'company_name',
'street_address',
'street_address_plus',
'zipcode',
'city',
'phone_number',
'is_default',
'type',
'user_id',
];
/**
* The dynamic attributes from mutators that should be returned with the user object.
*
* @var array
*/
protected $appends = [
'full_name',
];
/**
* Define if an address is default or not.
*/
public function isDefault(): bool
{
return $this->is_default === true;
}
/**
* User
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
/**
* Bootstrap the model and its traits.
*/
protected static function boot()
{
parent::boot();
static::creating(function ($address) {
if ($address->is_default) {
$address->user->addresses()->where('type', $address->type)->update([
'is_default' => false,
]);
}
});
}
}