ldc-crm/app/Models/Brand.php
2023-06-16 12:15:03 +02:00

151 lines
3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Rennokki\QueryCache\Traits\QueryCacheable;
class Brand extends Model
{
use HasFactory;
use QueryCacheable;
/**
* The timeout cache for this model
*
* @var integer
*/
protected $cacheFor = 0;
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'name'
];
/**
* 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
*/
protected function getCacheBaseTags(): array
{
return [
'crm_brands',
];
}
/**
* Perform any actions required after the model boots.
*
* @return void
*/
protected static function booted()
{
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 hasMany relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function services()
{
return $this->hasMany(Service::class);
}
/**
* Question hasMany relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function questions()
{
return $this->hasMany(Question::class);
}
/**
* CustomerInfo hasMany relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function customerInfos()
{
return $this->hasMany(CustomerInfo::class);
}
/**
* ServiceStat hasMany relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function servicesStats()
{
return $this->hasMany(ServiceStat::class);
}
/**
* SingleOptin hasMany relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function singleOptins()
{
return $this->hasMany(SingleOptin::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();
}
}