Files
monica/app/Models/Template.php

87 lines
2.1 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Template extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'account_id',
'name',
'name_translation_key',
'can_be_deleted',
];
/**
* The attributes that should be cast to native types.
*
* @var array<string, string>
*/
protected $casts = [
'can_be_deleted' => 'boolean',
];
/**
* Get the account associated with the template.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\Account, $this>
*/
public function account(): BelongsTo
{
return $this->belongsTo(Account::class);
}
/**
* Get the template page records associated with the template.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany<\App\Models\TemplatePage, $this>
*/
public function pages(): HasMany
{
return $this->hasMany(TemplatePage::class);
}
/**
* Get the contacts associated with the template.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany<\App\Models\Contact, $this>
*/
public function contacts(): HasMany
{
return $this->hasMany(Contact::class);
}
/**
* Get the name attribute.
* Templates have a default name that can be translated.
* Howerer, if a name is set, it will be used instead of the default.
*
* @return Attribute<string,string>
*/
protected function name(): Attribute
{
return Attribute::make(
get: function ($value, $attributes) {
if (! $value) {
return __($attributes['name_translation_key']);
}
return $value;
},
set: fn ($value) => $value,
);
}
}