ldc-groupe/app/Models/Company.php
2025-12-01 14:51:11 +01:00

149 lines
No EOL
4.5 KiB
PHP

<?php
namespace App\Models;
use App\Models\CompanyActivity;
use App\Models\CompanySector;
use App\Traits\Models\HasUwilImages;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Cache;
use Uwil\LaravelActivityLogs\Traits\ActivityLog;
/**
* @property integer $id Id.
* @property string $name Name of the company.
* @property string $logo Logo of the company.
* @property string $responsable Responsable of the company.
* @property string $url Link the website of the company.
* @property string $informations Informations of the company.
* @property string $employees Effectif number of of the company.
* @property string $latitude Latitude coordination of the company.
* @property string $longitude Longitude of the company.
* @property integer $activity_id Activity associated to the company.
* @property integer|null $sector_id Sector associated to the company.
* @property string $published Publish status of the company.
* @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 Company extends Model
{
use ActivityLog;
use HasFactory;
use SoftDeletes;
use HasUwilImages;
/**
* The attributes that are mass assignable.
*
* @var array<int, string, array[]>
*/
protected $fillable = [
'name',
'logo',
'responsable',
'url',
'informations',
'employees',
'latitude',
'longitude',
'activity_id',
'sector_id',
'published',
];
/**
* 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::created(function () {
self::resetCache();
});
static::updated(function () {
self::resetCache();
});
static::deleted(function () {
self::resetCache();
});
static::restored(function () {
self::resetCache();
});
static::forceDeleted(function () {
self::resetCache();
});
}
/**
* Reset all cache elements related to this model.
*
* @return void
*/
public static function resetCache(): void
{
Cache::forget('allCompanies');
self::getAllCompanies();
Cache::forget('publishedCompanies');
self::getPublishedCompanies();
}
/**
* Get all companies.
*
* @return \Illuminate\Database\Eloquent\Collection<int,self>
*/
public static function getAllCompanies(): \Illuminate\Database\Eloquent\Collection
{
return Cache::remember('allCompanies', 86400, function () {
return self::query()->with(['companyActivity', 'companySector'])->get();
});
}
/**
* Get all published companies.
*
* @return \Illuminate\Database\Eloquent\Collection<int,self>
*/
public static function getPublishedCompanies(): \Illuminate\Database\Eloquent\Collection
{
return Cache::remember('publishedCompanies', 86400, function () {
return self::getAllCompanies()->where('published', true);
});
}
// * RELATIONSHIPS
/**
* Get the activity for this company.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\CompanyActivity,$this>
*/
public function companyActivity(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(CompanyActivity::class, 'activity_id');
}
/**
* Get the sector for this company.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\CompanySector,$this>
*/
public function companySector(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(CompanySector::class, 'sector_id');
}
}