feat: ✨ add brand fields (logo/domain/url/url_claim)

This commit is contained in:
Alexis Gousseau 2025-08-07 16:07:58 +02:00
parent 30c113a30c
commit 36494a7944
No known key found for this signature in database
GPG key ID: 4A614CB3DBBA2406
11 changed files with 238 additions and 23 deletions

View file

@ -3,9 +3,12 @@
namespace App\Http\Requests\Bo\Brands;
use Illuminate\Foundation\Http\FormRequest;
use Uwil\LaravelCommon\Traits\Requests\HasPicture;
class StoreBrandRequest extends FormRequest
{
use HasPicture;
/**
* Determine if the user is authorized to make this request.
*
@ -16,6 +19,16 @@ class StoreBrandRequest extends FormRequest
return auth('backend')->check();
}
/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation(): void
{
$this->mergePicture('logo');
}
/**
* Get the validation rules that apply to the request.
*
@ -23,9 +36,16 @@ class StoreBrandRequest extends FormRequest
*/
public function rules(): array
{
return [
'name' => 'required|string|min:5|max:255',
$rules = [
'name' => 'required|unique:brands,name|string|min:5|max:255',
'url' => 'required|url|min:5|max:255',
'url_claim' => 'required|url|min:5|max:255',
'domain' => 'required|unique:brands,domain|string|min:5|max:255',
];
return \array_merge(
$rules,
$this->pictureRules('logo', 550, 550, 550, 550),
);
}
/**
@ -36,7 +56,11 @@ class StoreBrandRequest extends FormRequest
public function attributes(): array
{
return [
'name' => trans('Nom de la marque'),
'name' => trans('validation.attributes.name'),
'logo' => trans('validation.custom.logo'),
'url' => trans('validation.custom.url'),
'url_claim' => trans('validation.custom.url_claim'),
'domain' => trans('validation.custom.domain'),
];
}
}

View file

@ -4,4 +4,34 @@ namespace App\Http\Requests\Bo\Brands;
class UpdateBrandRequest extends StoreBrandRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
/** @var integer $brandModelKey */
$brandModelKey = request()->route()->parameter('brand')->getKey();
$rules = [
'name' => [
'required',
"unique:brands,name,$brandModelKey",
'string',
'min:5',
'max:255',
],
'domain' => [
'required',
"unique:brands,domain,$brandModelKey",
'string',
'min:5',
'max:255',
],
];
return \array_merge(
parent::rules(),
$rules,
);
}
}

View file

@ -7,6 +7,7 @@ use App\Traits\Models\ActivityLog;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Uwil\LaravelCommon\Helpers\FileHelper;
/**
* @property integer $id Brand id.
@ -54,14 +55,22 @@ class Brand extends Model
*/
protected static function booted(): void
{
static::creating(function (self $model) {
$model->logo = FileHelper::storeFile($model, $model->logo, true);
});
static::created(function () {
self::resetCache();
});
static::updated(function () {
self::resetCache();
static::updating(function (self $model) {
$model->logo = FileHelper::storeFile($model, $model->logo, true);
});
static::deleted(function () {
static::updated(function (self $model) {
self::resetCache();
FileHelper::removeOldFile($model, 'logo');
});
static::deleted(function (self $model) {
self::resetCache();
FileHelper::removeOldFile($model, 'logo');
});
}

View file

@ -22,10 +22,10 @@ class BrandFactory extends Factory
public function definition(): array
{
return [
'name' => $this->faker->company,
'name' => $this->faker->unique()->company,
'url' => $this->faker->url,
'url_claim' => $this->faker->url,
'domain' => $this->faker->domainName,
'domain' => $this->faker->unique()->domainName,
];
}
}

View file

@ -0,0 +1,52 @@
<?php
use App\Models\Brand;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Uwil\LaravelCommon\Helpers\FileHelper;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
// Set default value.
Brand::query()->get()->map(function (Brand $brandModel) {
$brandModel->logo = FileHelper::storeFile(
$brandModel,
new \SplFileInfo(
\resource_path('../database/factories/assets/brands/default-logo-550x550.jpg')
)
);
$urlExample = "https://example-{$brandModel->getKey()}.com";
$brandModel->url = $urlExample;
$brandModel->url_claim = $urlExample;
$brandModel->domain = parse_url($urlExample, PHP_URL_HOST);
$brandModel->saveOrFail();
});
// Set unique keys.
Schema::table('brands', function (Blueprint $table) {
$table->unique('name', 'brands_name_unique');
$table->unique('domain', 'brands_domain_unique');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::table('brands', function (Blueprint $table) {
$table->dropUnique('brands_name_unique');
$table->dropUnique('brands_domain_unique');
});
}
};

View file

@ -233,5 +233,9 @@ return [
'type' => 'type',
'status' => 'status',
'label' => 'label',
'domain' => 'domain',
'logo' => 'logo',
'url' => 'url',
'url_claim' => 'claim url',
],
];

View file

@ -233,5 +233,9 @@ return [
'type' => 'type',
'status' => 'status',
'label' => 'label',
'domain' => 'domaine',
'logo' => 'logo',
'url' => 'lien',
'url_claim' => 'lien réclamation',
],
];

View file

@ -29,7 +29,7 @@
</div>
</div>
<div class="card-body">
<form action="{{ route('bo.brands.store') }}" method="POST">
<form action="{{ route('bo.brands.store') }}" method="POST" enctype="multipart/form-data">
@csrf
@include('bo.pages.brands.form')
<div class="row">

View file

@ -29,7 +29,7 @@
</div>
</div>
<div class="card-body">
<form action="{{ route('bo.brands.update', $brandModel->id) }}" method="POST">
<form action="{{ route('bo.brands.update', $brandModel->id) }}" method="POST" enctype="multipart/form-data">
@csrf
@method('PUT')
@include('bo.pages.brands.form')

View file

@ -2,22 +2,111 @@
{{-- TITLE --}}
<div class="col-12 col-md-6 mb-3">
<fieldset class="h-100 border p-3">
<legend>{{ __('Nom') }}</legend>
<legend>{{ str(__('validation.attributes.name'))->ucfirst() }}</legend>
<div class="row">
<div class="col-12 mb-3">
@include('bo.partials.inputs.varchar', [
'id' => 'brandName',
'label' => __('Nom de la :model', ['model' => Str::singular(__('models.classes.brands'))]),
'label' => __('Nom de la :model', ['model' => str(__('models.classes.brands'))->singular()]),
'hasWordCounter' => true,
'name' => 'name',
'required' => true,
'model' => $brandModel ?? null,
'helper' => __('Doit décrire précisément la :model (3 caractères minimum, 255 maximum).', [
'model' => str(__('models.classes.brands'))->singular(),
]),
])
</div>
</div>
</fieldset>
</div>
{{-- DOMAIN --}}
<div class="col-12 col-md-6 mb-3">
<fieldset class="h-100 border p-3">
<legend>{{ str(__('validation.custom.domain'))->ucfirst() }}</legend>
<div class="row">
<div class="col-12 mb-3">
@include('bo.partials.inputs.varchar', [
'id' => 'brandDomain',
'label' => __(':field de la :model', [
'field' => str(__('validation.custom.domain'))->ucfirst(),
'model' => str(__('models.classes.brands'))->singular(),
]),
'hasWordCounter' => true,
'name' => 'domain',
'required' => true,
'model' => $brandModel ?? null,
'placeholder' => parse_url('https://example.com/', PHP_URL_HOST),
'helper' => __('Doit décrire précisément le produit (3 caractères minimum, 255 maximum).'),
])
</div>
</div>
</fieldset>
</div>
{{-- URL --}}
<div class="col-12 col-md-6 mb-3">
<fieldset class="h-100 border p-3">
<legend>{{ str(__('validation.custom.url'))->ucfirst() }}</legend>
<div class="row">
<div class="col-12 mb-3">
@include('bo.partials.inputs.varchar', [
'id' => 'brandUrl',
'label' => __(':field de la :model', [
'field' => str(__('validation.custom.url'))->ucfirst(),
'model' => str(__('models.classes.brands'))->singular(),
]),
'hasWordCounter' => true,
'name' => 'url',
'required' => true,
'model' => $brandModel ?? null,
'placeholder' => 'https://example.com/',
'helper' => __('Doit décrire précisément le produit (3 caractères minimum, 255 maximum).'),
])
</div>
</div>
</fieldset>
</div>
{{-- URL_CLAIM --}}
<div class="col-12 col-md-6 mb-3">
<fieldset class="h-100 border p-3">
<legend>{{ str(__('validation.custom.url_claim'))->ucfirst() }}</legend>
<div class="row">
<div class="col-12 mb-3">
@include('bo.partials.inputs.varchar', [
'id' => 'brandUrlClaim',
'label' => __(':field de la :model', [
'field' => str(__('validation.custom.url_claim'))->ucfirst(),
'model' => str(__('models.classes.brands'))->singular(),
]),
'hasWordCounter' => true,
'name' => 'url_claim',
'required' => true,
'model' => $brandModel ?? null,
'placeholder' => 'https://example.com/',
'helper' => __('Doit décrire précisément le produit (3 caractères minimum, 255 maximum).'),
])
</div>
</div>
</fieldset>
</div>
<div class="col-12 mb-3">
<fieldset class="h-100 border p-3">
<legend>{{ str(__('validation.custom.url_claim'))->ucfirst() }}</legend>
<div class="row">
<div class="col-12">
@include('bo.partials.inputs.varchar-picture', [
'id' => 'brandLogo',
'name' => 'logo',
'width' => 550,
'height' => 550,
'model' => $brandModel ?? null,
'required' => true,
'preview' => true,
])
</div>
</div>
</fieldset>
</div>
</div>
@push('scripts')

View file

@ -15,17 +15,20 @@
--}}
@php
$data = [
'id' => $id,
'name' => $name,
'helper' => $helper,
'width' => $width,
'height' => $height,
'value' => $model->{$name} ?? $value ?? '',
'preview' => $preview ?? false,
'browseEvent' => $browseEvent ?? false,
'required' => $required ?? false,
];
$data = [
'id' => $id,
'name' => $name,
'helper' => __('crud.minicrud.dimensions_must_be', [
'widthpx' => "{$width}px",
'heightpx' => "{$height}px",
]),
'width' => $width,
'height' => $height,
'value' => $model->{$name} ?? ($value ?? ''),
'preview' => $preview ?? false,
'browseEvent' => $browseEvent ?? false,
'required' => $required ?? false,
];
@endphp
<div class="image-input" data-json='@json($data)'></div>
@include('modules.input-error', ['inputName' => $name, 'helper' => null])