Files
project/app/Models/Customer.php

80 lines
1.6 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',
];
/**
* Get the created_at attribute in local time format.
*
* @return string
*/
public function getCreatedAttribute(): string
{
return $this->created_at->format('d.m.Y');
}
/**
* Get the invoice address.
*
* @return HasOne
*/
public function address(): HasOne
{
return $this->hasOne(Address::class)->ofMany([],
function (Builder $query) {
$query->where('is_address', '=', true);
});
}
/**
* Get the delivery address.
*
* @return HasOne
*/
public function delivery(): HasOne
{
return $this->hasOne(Address::class)->ofMany([],
function (Builder $query) {
$query->where('is_delivery', '=', true);
});
}
/**
* Get all customer's addresses.
*
* @return HasMany
*/
public function addresses(): HasMany
{
return $this->hasMany(Address::class);
}
}