2020-12-01 17:02:10 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-09-08 16:43:44 +02:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
use Kwaadpepper\CrudPolicies\Enums\CrudAction;
|
|
|
|
|
use Kwaadpepper\CrudPolicies\Enums\CrudType;
|
|
|
|
|
use Kwaadpepper\CrudPolicies\Traits\IsCrudModel;
|
2021-08-23 20:03:17 +02:00
|
|
|
use Rennokki\QueryCache\Traits\QueryCacheable;
|
2020-12-01 17:02:10 +01:00
|
|
|
|
|
|
|
|
class Brand extends Model
|
|
|
|
|
{
|
2021-09-08 16:43:44 +02:00
|
|
|
use IsCrudModel;
|
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
|
|
|
/** @var string */
|
2021-09-08 16:43:44 +02:00
|
|
|
protected $crudLabelColumn = 'name';
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/** @var string */
|
2021-09-08 16:43:44 +02:00
|
|
|
protected $crudValueColumn = 'id';
|
|
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
/**
|
|
|
|
|
* The timeout cache for this model
|
|
|
|
|
*
|
|
|
|
|
* @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 = [
|
|
|
|
|
'name'
|
|
|
|
|
];
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
self::$editableProperties = [
|
|
|
|
|
'name' => [
|
|
|
|
|
'label' => Str::ucfirst(trans('models.common.name')),
|
|
|
|
|
'type' => CrudType::string(),
|
|
|
|
|
'validate' => [
|
|
|
|
|
'min:5',
|
|
|
|
|
'max:255'
|
|
|
|
|
],
|
|
|
|
|
'actions' => [
|
|
|
|
|
CrudAction::viewAny(),
|
|
|
|
|
CrudAction::view(),
|
|
|
|
|
CrudAction::create(),
|
|
|
|
|
CrudAction::update()
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
];
|
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
|
|
|
|
|
*/
|
2021-08-23 20:03:17 +02:00
|
|
|
protected static function booted()
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
2021-08-11 17:25:24 +02:00
|
|
|
/**
|
|
|
|
|
* Question hasMany relation
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
*/
|
|
|
|
|
public function questions()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(Question::class);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-28 12:04:49 +01:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
}
|
|
|
|
|
|
2021-02-28 12:04:49 +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
|
|
|
|
2021-02-28 12:04:49 +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-08 11:37:54 +01:00
|
|
|
|
2021-12-19 15:57:24 +01:00
|
|
|
// * METHODS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Account attribute accessor
|
|
|
|
|
*
|
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
|
* Total optins attribute accessor
|
|
|
|
|
*
|
|
|
|
|
* @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
|
|
|
}
|