ldc-crm/app/Models/SingleOptin.php

87 lines
2.7 KiB
PHP
Raw Permalink 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 Illuminate\Support\Str;
use Kwaadpepper\CrudPolicies\Enums\CrudAction;
use Kwaadpepper\CrudPolicies\Enums\CrudType;
use Kwaadpepper\CrudPolicies\Traits\IsCrudModel;
2020-12-01 17:02:10 +01:00
2020-12-02 10:08:27 +01:00
class SingleOptin extends Model
2020-12-01 17:02:10 +01:00
{
use HasFactory;
use IsCrudModel;
protected $crudLabelColumn = 'email';
protected $crudValueColumn = 'id';
2020-12-01 17:02:10 +01:00
2021-03-14 10:45:42 +01:00
protected $fillable = [
'email'
];
public function __construct(array $attributes = [])
{
self::$editableProperties = [
'email' => [
'label' => Str::ucfirst(trans('models.common.email')),
'type' => CrudType::email(),
'validate' => [
'required',
'email:rfc,dns',
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
function ($attribute, $value, $fail) {
$q = static::query();
if (request()->user) {
$q = $q->where('id', '!=', $this->id);
}
if ($q->where('email', $value)->exists()) {
$fail(__(':attribute est déjà utilisé', [
'attribute' => $attribute
]));
}
}
],
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::create(),
CrudAction::update()
]
],
'brand_id' => [
2021-09-09 15:15:23 +02:00
'label' => Str::ucfirst(trans('models.classes.brand')),
'type' => CrudType::belongsTo(),
'belongsTo' => Brand::class,
'actions' => [
CrudAction::viewAny(),
CrudAction::view()
]
],
'created_at' => [
'label' => Str::ucfirst(trans('models.common.optinDate')),
'type' => CrudType::string(),
'readonly' => true,
'actions' => [
CrudAction::viewAny(),
CrudAction::view(),
CrudAction::update()
]
],
];
parent::__construct($attributes);
}
/**
* Brand BelongsTo relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function brand()
{
return $this->belongsTo(Brand::class);
2020-12-01 17:02:10 +01:00
}
}