You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.5 KiB
67 lines
1.5 KiB
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
use App\Models\Company;
|
|
|
|
class City extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'uuid',
|
|
'name',
|
|
'url',
|
|
];
|
|
|
|
public function companies(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Company::class, 'cities_companies', 'city_id', 'company_id');
|
|
}
|
|
|
|
public function userAddresses(): HasMany
|
|
{
|
|
return $this->hasMany(UserAddress::class);
|
|
}
|
|
|
|
public function getCompanyIds(): array
|
|
{
|
|
return $this->companies()->pluck('company_id')->toArray();
|
|
}
|
|
|
|
public function getCompanies()
|
|
{
|
|
return $this->companies;
|
|
}
|
|
|
|
public function addCompanyId(int $company_id)
|
|
{
|
|
if(Company::isExist($company_id))
|
|
$this->addCompanyIds([$company_id]);
|
|
}
|
|
|
|
public function addCompanyIds(array $company_ids)
|
|
{
|
|
$existingCompanies = array_filter($company_ids, ['App\Models\Company', 'isExist']);
|
|
|
|
$companyIDs = array_merge($this->getCompanyIds(), $existingCompanies);
|
|
|
|
$this->companies()->sync($companyIDs);
|
|
}
|
|
|
|
public function removeCompanyId(int $company_id)
|
|
{
|
|
$this->companies()->detach($company_id);
|
|
}
|
|
|
|
public function removeCompanyIds(array $company_ids)
|
|
{
|
|
$this->companies()->detach($company_ids);
|
|
}
|
|
}
|