ldc-crm/app/Models/Brand.php

117 lines
2.5 KiB
PHP
Raw Normal View History

2020-12-01 17:02:10 +01:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Rennokki\QueryCache\Traits\QueryCacheable;
2020-12-01 17:02:10 +01:00
class Brand extends Model
{
use HasFactory;
use QueryCacheable;
protected $cacheFor = 0;
2020-12-01 17:02:10 +01:00
2021-03-14 10:45:42 +01:00
protected $fillable = [
'name'
];
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',
];
}
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();
});
}
/**
* Service hasMany relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function services()
{
return $this->hasMany(Service::class);
2020-12-01 17:02:10 +01:00
}
/**
* 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);
2020-12-01 17:02:10 +01:00
}
/**
* ServiceStat hasMany relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function servicesStats()
{
return $this->hasMany(ServiceStat::class);
2020-12-01 17:02:10 +01:00
}
2020-12-02 10:08:27 +01:00
/**
* SingleOptin hasMany relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function singleOptins()
{
return $this->hasMany(SingleOptin::class);
2020-12-02 10:08:27 +01:00
}
2021-03-14 10:45:42 +01:00
public function getAccountsAttribute()
{
return $this->customerInfos()->count();
}
2021-03-14 10:45:42 +01:00
public function getTotalOptinsAttribute()
{
return $this->customerInfos()->where('opt_in', 1)->count() + $this->singleOptins()->count();
}
2020-12-01 17:02:10 +01:00
}