2020-12-01 17:02:10 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2023-10-06 15:00:42 +02:00
|
|
|
use App\Traits\Models\ActivityLog;
|
2020-12-01 17:02:10 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-08-23 20:03:17 +02:00
|
|
|
use Rennokki\QueryCache\Traits\QueryCacheable;
|
2020-12-01 17:02:10 +01:00
|
|
|
|
2023-09-06 17:24:53 +02:00
|
|
|
/**
|
|
|
|
|
* @property integer $id Id.
|
|
|
|
|
* @property string $name Name.
|
|
|
|
|
* @property-read \Illuminate\Support\Carbon $created_at Created date.
|
|
|
|
|
* @property-read \Illuminate\Support\Carbon $updated_at 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.
|
|
|
|
|
*/
|
2020-12-01 17:02:10 +01:00
|
|
|
class Brand extends Model
|
|
|
|
|
{
|
2023-10-06 15:00:42 +02:00
|
|
|
use ActivityLog;
|
2020-12-01 17:02:10 +01:00
|
|
|
use HasFactory;
|
2021-08-23 20:03:17 +02:00
|
|
|
use QueryCacheable;
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
2023-09-06 17:24:53 +02:00
|
|
|
* The timeout cache for this model.
|
2021-12-19 15:57:24 +01:00
|
|
|
*
|
|
|
|
|
* @var integer
|
|
|
|
|
*/
|
2021-08-23 20:03:17 +02:00
|
|
|
protected $cacheFor = 0;
|
2020-12-01 17:02:10 +01:00
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
|
|
|
|
* @var string[]
|
|
|
|
|
*/
|
2021-03-14 10:45:42 +01:00
|
|
|
protected $fillable = [
|
2023-09-04 13:08:34 +02:00
|
|
|
'name',
|
2021-03-14 10:45:42 +01:00
|
|
|
];
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Brand
|
|
|
|
|
*
|
|
|
|
|
* @param array $attributes
|
|
|
|
|
*/
|
2021-08-23 20:03:17 +02:00
|
|
|
public function __construct(array $attributes = [])
|
|
|
|
|
{
|
|
|
|
|
$this->cacheFor = config('api.servicesDBCache') * 60;
|
2021-09-08 16:43:44 +02:00
|
|
|
|
2021-08-23 20:03:17 +02:00
|
|
|
parent::__construct($attributes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the base cache tags that will be present
|
|
|
|
|
* on all queries.
|
|
|
|
|
*
|
2021-12-19 15:57:24 +01:00
|
|
|
* ! This is used to flush caching
|
2021-08-23 20:03:17 +02:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function getCacheBaseTags(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'crm_brands',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* Perform any actions required after the model boots.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2023-09-06 17:24:53 +02:00
|
|
|
protected static function booted(): void
|
2021-08-23 20:03:17 +02:00
|
|
|
{
|
|
|
|
|
static::created(function () {
|
2021-12-19 15:57:24 +01:00
|
|
|
// * Flush Eloquent cache.
|
2021-08-23 20:03:17 +02:00
|
|
|
self::flushQueryCache();
|
|
|
|
|
});
|
|
|
|
|
static::updated(function () {
|
2021-12-19 15:57:24 +01:00
|
|
|
// * Flush Eloquent cache.
|
2021-08-23 20:03:17 +02:00
|
|
|
self::flushQueryCache();
|
|
|
|
|
});
|
|
|
|
|
static::deleted(function () {
|
2021-12-19 15:57:24 +01:00
|
|
|
// * Flush Eloquent cache.
|
2021-08-23 20:03:17 +02:00
|
|
|
self::flushQueryCache();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
// * RELATIONS
|
|
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
/**
|
2023-09-06 17:24:53 +02:00
|
|
|
* Service hasMany relation.
|
2021-02-28 12:04:49 +01:00
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
*/
|
2023-09-06 17:24:53 +02:00
|
|
|
public function services(): \Illuminate\Database\Eloquent\Relations\HasMany
|
2021-02-28 12:04:49 +01:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(Service::class);
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
2021-08-11 17:25:24 +02:00
|
|
|
/**
|
2023-09-06 17:24:53 +02:00
|
|
|
* Question hasMany relation.
|
2021-08-11 17:25:24 +02:00
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
*/
|
2023-09-06 17:24:53 +02:00
|
|
|
public function questions(): \Illuminate\Database\Eloquent\Relations\HasMany
|
2021-08-11 17:25:24 +02:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(Question::class);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
/**
|
2023-09-06 17:24:53 +02:00
|
|
|
* CustomerInfo hasMany relation.
|
2021-02-28 12:04:49 +01:00
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
*/
|
2023-09-06 17:24:53 +02:00
|
|
|
public function customerInfos(): \Illuminate\Database\Eloquent\Relations\HasMany
|
2021-02-28 12:04:49 +01:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(CustomerInfo::class);
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
/**
|
2023-09-06 17:24:53 +02:00
|
|
|
* ServiceStat hasMany relation.
|
2021-02-28 12:04:49 +01:00
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
*/
|
2023-09-06 17:24:53 +02:00
|
|
|
public function servicesStats(): \Illuminate\Database\Eloquent\Relations\HasMany
|
2021-02-28 12:04:49 +01:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(ServiceStat::class);
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|
2020-12-02 10:08:27 +01:00
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
/**
|
2023-09-06 17:24:53 +02:00
|
|
|
* SingleOptin hasMany relation.
|
2021-02-28 12:04:49 +01:00
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
*/
|
2023-09-06 17:24:53 +02:00
|
|
|
public function singleOptins(): \Illuminate\Database\Eloquent\Relations\HasMany
|
2021-02-28 12:04:49 +01:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(SingleOptin::class);
|
2020-12-02 10:08:27 +01:00
|
|
|
}
|
2021-03-08 11:37:54 +01:00
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
// * METHODS
|
|
|
|
|
|
|
|
|
|
/**
|
2023-09-06 17:24:53 +02:00
|
|
|
* Account attribute accessor.
|
2021-12-19 15:57:24 +01:00
|
|
|
*
|
|
|
|
|
* @return integer
|
|
|
|
|
*/
|
|
|
|
|
public function getAccountsAttribute(): int
|
2021-03-14 10:45:42 +01:00
|
|
|
{
|
|
|
|
|
return $this->customerInfos()->count();
|
2021-03-08 11:37:54 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
2023-09-06 17:24:53 +02:00
|
|
|
* Total optins attribute accessor.
|
2021-12-19 15:57:24 +01:00
|
|
|
*
|
|
|
|
|
* @return integer
|
|
|
|
|
*/
|
|
|
|
|
public function getTotalOptinsAttribute(): int
|
2021-03-14 10:45:42 +01:00
|
|
|
{
|
|
|
|
|
return $this->customerInfos()->where('opt_in', 1)->count() + $this->singleOptins()->count();
|
2021-03-08 11:37:54 +01:00
|
|
|
}
|
2020-12-01 17:02:10 +01:00
|
|
|
}
|