ldc-crm/app/Models/Brand.php
Jérémy Munsch 72a5b73902 overhaull UI upgrade
using crud policies
2021-09-09 16:08:37 +02:00

153 lines
3.7 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Kwaadpepper\CrudPolicies\Enums\CrudAction;
use Kwaadpepper\CrudPolicies\Enums\CrudType;
use Kwaadpepper\CrudPolicies\Traits\IsCrudModel;
use Rennokki\QueryCache\Traits\QueryCacheable;
class Brand extends Model
{
use IsCrudModel;
use HasFactory;
use QueryCacheable;
protected $crudLabelColumn = 'name';
protected $crudValueColumn = 'id';
protected $cacheFor = 0;
protected $fillable = [
'name'
];
public function __construct(array $attributes = [])
{
$this->cacheFor = config('api.servicesDBCache') * 60;
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()
]
],
'services' => [
'label' => Str::ucfirst(Str::plural(trans('models.models.service'))),
'type' => CrudType::belongsToMany(),
'belongsToMany' => Service::class,
'belongsToManyLabel' => 'name',
'actions' => [
CrudAction::view(),
CrudAction::create(),
CrudAction::update(),
]
]
];
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);
}
/**
* 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);
}
public function getAccountsAttribute()
{
return $this->customerInfos()->count();
}
public function getTotalOptinsAttribute()
{
return $this->customerInfos()->where('opt_in', 1)->count() + $this->singleOptins()->count();
}
}