122 lines
3.2 KiB
PHP
122 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Contracts\TranslatableModel;
|
|
use App\Helpers\TranslationHelper;
|
|
use App\Models\Company;
|
|
use App\Traits\Models\HasCache;
|
|
use App\Traits\Models\HasTranslations;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Uwil\LaravelActivityLogs\Traits\ActivityLog;
|
|
|
|
/**
|
|
* @property integer $id Id.
|
|
* @property string $name Name of the company sector.
|
|
* @property string $published Publish status of the company sector.
|
|
* @property-read \Carbon\Carbon $created_at Created date.
|
|
* @property-read \Carbon\Carbon $updated_at Updated date.
|
|
* @property-read \Illuminate\Support\Carbon $deleted_at Deleted date.
|
|
*/
|
|
class CompanySector extends Model implements TranslatableModel
|
|
{
|
|
use ActivityLog;
|
|
use HasCache;
|
|
use HasFactory;
|
|
use HasTranslations;
|
|
use SoftDeletes;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'published',
|
|
];
|
|
|
|
/**
|
|
* The translatable columns.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
public $translatable = [
|
|
'name',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'published' => 'boolean',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::deleting(function (self $model) {
|
|
if ($model->companies()->exists()) {
|
|
throw ValidationException::withMessages([
|
|
'sector_id' => trans('crud.messages.cannot_delete_relation'),
|
|
]);
|
|
}
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Manage model fields.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(self $model): void
|
|
{
|
|
TranslationHelper::assertTranslationFieldIsUnique($model, $model->name, 'name');
|
|
self::validateFallbackTranslations($model);
|
|
}
|
|
|
|
/**
|
|
* Assert fallback locale translation is present.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException Translations missing.
|
|
*/
|
|
public static function validateFallbackTranslations(self $model): void
|
|
{
|
|
if (!$model->published) {
|
|
return;
|
|
}
|
|
TranslationHelper::validateFallbackTranslations($model);
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* Get companies for this sector.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function companies(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(Company::class, 'sector_id');
|
|
}
|
|
}
|