218 lines
5.7 KiB
PHP
218 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Traits\Models\ActivityLog;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Rennokki\QueryCache\Traits\QueryCacheable;
|
|
|
|
/**
|
|
* @property integer $id Brand id.
|
|
* @property string $name Brand name.
|
|
* @property string $logo Brand logo.
|
|
* @property string $url Brand url link.
|
|
* @property string $url_claim Brand associative url claim.
|
|
* @property string $domain Brand domain.
|
|
* @property-read \Illuminate\Support\Carbon $created_at Brand created date.
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Brand updated date.
|
|
*
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<App\Models\Service>
|
|
* $services Service HasMany relation.
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<App\Models\Question>
|
|
* $questions Question HasMany relation.
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<App\Models\CustomerInfo>
|
|
* $customerInfos CustomerInfo HasMany relation.
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<App\Models\ServicesStat>
|
|
* $servicesStats ServicesStat HasMany relation.
|
|
* @property-read \Illuminate\Database\Eloquent\Collection<App\Models\SingleOptin>
|
|
* $singleOptins SingleOptin HasMany relation.
|
|
*/
|
|
class Brand extends Model
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
use QueryCacheable;
|
|
|
|
/**
|
|
* The timeout cache for this model.
|
|
*
|
|
* @var integer
|
|
*/
|
|
protected $cacheFor = 0;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'logo',
|
|
'url',
|
|
'url_claim',
|
|
'domain',
|
|
];
|
|
|
|
/**
|
|
* Brand
|
|
*
|
|
* @param array $attributes
|
|
*/
|
|
public function __construct(array $attributes = [])
|
|
{
|
|
$this->cacheFor = config('api.servicesDBCache') * 60;
|
|
|
|
parent::__construct($attributes);
|
|
}
|
|
|
|
/**
|
|
* Set the base cache tags that will be present
|
|
* on all queries.
|
|
*
|
|
* ! This is used to flush caching
|
|
*
|
|
* @return array<int, string>
|
|
*/
|
|
protected function getCacheBaseTags(): array
|
|
{
|
|
return [
|
|
'crm_brands',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::created(function () {
|
|
// * Flush Eloquent cache.
|
|
self::flushQueryCache();
|
|
});
|
|
static::updated(function () {
|
|
// * Flush Eloquent cache.
|
|
self::flushQueryCache();
|
|
});
|
|
static::deleted(function () {
|
|
// * Flush Eloquent cache.
|
|
self::flushQueryCache();
|
|
});
|
|
}
|
|
|
|
// * RELATIONS
|
|
|
|
/**
|
|
* Service has many relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function services(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(Service::class);
|
|
}
|
|
|
|
/**
|
|
* Question has many relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function questions(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(Question::class);
|
|
}
|
|
|
|
/**
|
|
* CustomerInfo has many relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function customerInfos(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(CustomerInfo::class);
|
|
}
|
|
|
|
/**
|
|
* ServiceStat has many relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function servicesStats(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(ServiceStat::class);
|
|
}
|
|
|
|
/**
|
|
* SingleOptin has many relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function singleOptins(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(SingleOptin::class);
|
|
}
|
|
|
|
/**
|
|
* Users many to many relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function users(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
{
|
|
return $this->belongsToMany(User::class, 'brand_users');
|
|
}
|
|
|
|
/**
|
|
* Claim has many relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function claims(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(Claim::class);
|
|
}
|
|
|
|
/**
|
|
* ClaimSubject has many relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function claimSubjects(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(ClaimSubject::class);
|
|
}
|
|
|
|
/**
|
|
* ClaimMailTemplate has many relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function claimMailTemplates(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(ClaimMailTemplate::class);
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Account attribute accessor.
|
|
*
|
|
* @return integer
|
|
*/
|
|
public function getAccountsAttribute(): int
|
|
{
|
|
return $this->customerInfos()->count();
|
|
}
|
|
|
|
/**
|
|
* Total optins attribute accessor.
|
|
*
|
|
* @return integer
|
|
*/
|
|
public function getTotalOptinsAttribute(): int
|
|
{
|
|
return $this->customerInfos()->where('opt_in', 1)->count() + $this->singleOptins()->count();
|
|
}
|
|
}
|