Files
project/app/Models/Customer.php
2025-02-03 14:41:53 +01:00

104 lines
2.2 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Builder;
class Customer extends Model
{
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
];
/**
* The attributes that are appended with attribute getters.
*
* @var string[]
*/
protected $appends = [
'created',
'street',
'city'
];
/**
* Deliver the customer's address as street directly with the model.
*/
public function getStreetAttribute(): string
{
if (is_null($this->address)) {
return '';
}
return $this->address->address;
}
/**
* Deliver the customer's address as city directly with the model.
*/
public function getCityAttribute(): string
{
if (is_null($this->address)) {
return '';
}
return $this->address->city;
}
/**
* Get the created_at attribute in local time format.
*/
public function getCreatedAttribute(): string
{
return $this->created_at->format('d.m.Y');
}
/**
* Get the invoice address.
*/
public function address(): HasOne
{
return $this->hasOne(Address::class)->ofMany([],
function (Builder $query) {
$query->where('is_address', '=', true);
});
}
/**
* Get the delivery address.
*/
public function delivery(): HasOne
{
return $this->hasOne(Address::class)->ofMany([],
function (Builder $query) {
$query->where('is_delivery', '=', true);
});
}
/**
* Get all customer's addresses.
*/
public function addresses(): HasMany
{
return $this->hasMany(Address::class);
}
/**
* Get all customer's invoices.
*/
public function invoices(): HasMany
{
return $this->hasMany(Invoice::class);
}
}