$accounts * @property-read \Illuminate\Database\Eloquent\Collection $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); } }