86 lines
2 KiB
PHP
86 lines
2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Credit\Account;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* @property int $id Doit correspondre aux ids marques de la CRM
|
|
* @property string $name Nom de la marque
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, Account> $accounts
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Site> $sites
|
|
* @mixin \Eloquent
|
|
*/
|
|
class Brand extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are fillable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'name'
|
|
];
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Update sites with the relationship.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return void
|
|
*/
|
|
public static function updateSites(Brand $brand): void
|
|
{
|
|
static::deleteSites($brand);
|
|
if (request()->sites) {
|
|
foreach (request()->sites as $siteId) {
|
|
$site = Site::where('id', $siteId)->firstOrFail();
|
|
$site->brand_id = $brand->id;
|
|
$site->saveOrFail();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Delete all sites with the relationship.
|
|
*
|
|
* @param \App\Models\Brand $brand
|
|
* @return void
|
|
*/
|
|
public static function deleteSites(Brand $brand): void
|
|
{
|
|
foreach ($brand->sites as $site) {
|
|
$site->brand_id = null;
|
|
$site->saveOrFail();
|
|
}
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* HasMany Site relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function sites()
|
|
{
|
|
return $this->hasMany(Site::class);
|
|
}
|
|
|
|
/**
|
|
* HasMany Account relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function accounts()
|
|
{
|
|
return $this->hasMany(Account::class);
|
|
}
|
|
}
|