ldc-supportweb/app/Models/Site.php
2026-05-26 12:15:12 +02:00

141 lines
4.1 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Uwil\LaravelActivityLogs\Traits\HasActivityLog;
use Uwil\LaravelBackend\Traits\Models\HasCache;
use Uwil\LaravelBackend\Traits\Models\HasModelNaming;
use Uwil\LaravelCommon\Helpers\ToolboxHelper;
use Uwil\LaravelI18n\Traits\Models\HasTranslations;
/**
* @property integer $id Primary key of the site.
* @property string $name Name of the site.
* @property string $url_re7 Re7 url of the site.
* @property string $url_prod Prod url of the site.
* @property \App\Models\User $project_manager_id Project manager associated.
* @property-read \Illuminate\Support\Carbon $created_at Date the site was created.
* @property-read \Illuminate\Support\Carbon $updated_at Date the site was updated.
* @property-read \Illuminate\Support\Carbon $deleted_at Date the site was deleted.
*/
class Site extends Model
{
use HasActivityLog;
use HasCache;
/** @use \Illuminate\Database\Eloquent\Factories\HasFactory<\Database\Factories\SiteFactory> */
use HasFactory;
use HasModelNaming;
use HasTranslations;
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'name',
'url_re7',
'url_prod',
'project_manager_id',
'gitlab_project_id',
];
/**
* The translatable columns.
*
* @var list<string>
*/
public $translatable = [
'name',
];
/**
* Relations should be included in the cache.
*
* @var list<string>
*/
public static $cacheRelations = [
'projectManager',
'gitlabProject',
'users',
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
#[\Override]
protected static function booted(): void
{
$assertUrls = function (self $model) {
if ($model->url_re7) {
ToolboxHelper::assertNoInternalUrls('url_re7', $model->url_re7);
}
if ($model->url_prod) {
ToolboxHelper::assertNoInternalUrls('url_prod', $model->url_prod);
}
};
static::creating($assertUrls);
static::updating($assertUrls);
}
// * RELATIONS.
/**
* Get users associated to the site.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany<\App\Models\User>
*/
public function users(): \Illuminate\Database\Eloquent\Relations\MorphToMany
{
return $this->morphedByMany(
User::class,
'sitable',
'sitables',
'site_id',
'sitable_id',
)->withPivot('created_at');
}
/**
* Get brands associated to the site.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany<\App\Models\Brand>
*/
public function brands(): \Illuminate\Database\Eloquent\Relations\MorphToMany
{
return $this->morphedByMany(
Brand::class,
'sitable',
'sitables',
'site_id',
'sitable_id',
)->withPivot('created_at');
}
/**
* Get project manager of the site.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\User,\App\Models\Site>
*/
public function projectManager(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(User::class, 'project_manager_id');
}
/**
* Get project manager of the site.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\GitlabProject,\App\Models\Site>
*/
public function gitlabProject(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(GitlabProject::class, 'gitlab_project_id');
}
}