Skip to content
Snippets Groups Projects
Verified Commit d350079a authored by Sofiane Lasri's avatar Sofiane Lasri
Browse files

feat: add slug field to prestations

- Added a 'slug' field to the prestations table with a migration.
- Updated the Prestation model to include 'slug' in the fillable attributes.
- Modified the PrestationController to require and handle the 'slug' input in create and update methods.
- Updated frontend forms to include a slug input for prestations.
- Adjusted tests to validate the presence of the slug in the prestation creation and update processes.
parent 0a554cf2
Branches
No related tags found
1 merge request!26Resolve "Ajouter slug automatique"
Pipeline #783 failed
......@@ -91,6 +91,7 @@ public function store(Request $request): RedirectResponse
$request->validate([
'lang' => 'required|string|in:fr,en',
'name' => 'required|string',
'slug' => 'required|string',
'category_id' => 'required|exists:categories,id',
'icon_svg' => 'sometimes|file|mimes:svg',
'attract_desc' => 'required|string',
......@@ -108,6 +109,7 @@ public function store(Request $request): RedirectResponse
}
}
$slug = Str::slug($request->input('slug'));
$nameTransKey = TranslationKey::create(['key' => 'prestation_name_'.Str::uuid()]);
Translation::create([
'translation_key_id' => $nameTransKey->id,
......@@ -131,6 +133,7 @@ public function store(Request $request): RedirectResponse
$prestation = Prestation::create([
'name_trans_key_id' => $nameTransKey->id,
'slug' => $slug,
'category_id' => $request->input('category_id'),
'icon_svg' => $iconSvgContent,
'attract_desc_trans_key_id' => $attractDescTransKey->id,
......@@ -151,6 +154,7 @@ public function update(Request $request, int $id): RedirectResponse
$request->validate([
'lang' => 'required|string|in:fr,en',
'name' => 'required|string',
'slug' => 'required|string',
'category_id' => 'required|exists:categories,id',
'icon_svg' => 'sometimes|file|mimes:svg',
'attract_desc' => 'required|string',
......@@ -168,6 +172,7 @@ public function update(Request $request, int $id): RedirectResponse
$prestation->update(['icon_svg' => $iconSvgContent]);
}
$slug = Str::slug($request->input('slug'));
$nameTransKey = $prestation->nameTransKey;
Translation::updateOrCreate(
['translation_key_id' => $nameTransKey->id, 'locale' => $request->input('lang')],
......@@ -187,6 +192,7 @@ public function update(Request $request, int $id): RedirectResponse
);
$prestation->update([
'slug' => $slug,
'category_id' => $request->input('category_id'),
'detail' => $request->input('detail', ''),
]);
......
......
......@@ -11,6 +11,7 @@ class Prestation extends CustomBaseModel
protected $fillable = [
'name_trans_key_id',
'slug',
'optional_precision_trans_key_id',
'icon_svg',
'attract_desc_trans_key_id',
......
......
......@@ -15,6 +15,7 @@ class PrestationFactory extends Factory
public function definition(): array
{
return [
'slug' => $this->faker->slug(),
'icon_svg' => $this->faker->word(),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
......
......
<?php
use App\Models\Prestation;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('prestations', function (Blueprint $table) {
$table->string('slug')->default('')->after('icon_svg');
});
$prestations = Prestation::all();
foreach ($prestations as $prestation) {
$prestation->slug = Str::slug($prestation->nameTransKey->getTranslation('fr') ?? uniqid());
$prestation->save();
}
Schema::table('prestations', function (Blueprint $table) {
$table->string('slug')->unique()->change();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('prestations', function (Blueprint $table) {
$table->dropColumn('slug');
});
}
};
......@@ -66,6 +66,7 @@ class="mb-3"
/>
<x-bs.input
id="prestation-name"
label="Nom de la prestation"
name="name"
class="mb-3"
......@@ -73,6 +74,14 @@ class="mb-3"
required
/>
<x-bs.slug-input
label="Slug"
name="slug"
sourceInputId="prestation-name"
class="mb-3"
:value="old('slug', !empty($prestation) ? $prestation->slug : '')"
required/>
<x-bs.select
id="category-select"
label="Catégorie"
......
......
......@@ -49,6 +49,7 @@ public function it_can_store_a_new_prestation()
$data = [
'lang' => 'fr',
'name' => 'Test Prestation',
'slug' => 'test-prestation',
'category_id' => $category->id,
'icon_svg' => UploadedFile::fake()->createWithContent('icon.svg', '<svg></svg>'),
'attract_desc' => 'Attractive description',
......@@ -89,6 +90,7 @@ public function it_can_update_a_prestation()
$data = [
'lang' => 'fr',
'name' => 'Updated Prestation',
'slug' => 'updated-prestation',
'category_id' => $category->id,
'icon_svg' => UploadedFile::fake()->createWithContent('icon.svg', '<svg></svg>'),
'attract_desc' => 'Updated description',
......@@ -104,6 +106,7 @@ public function it_can_update_a_prestation()
$this->assertDatabaseHas('prestations', [
'id' => $prestation->id,
'detail' => 'Updated details',
'slug' => 'updated-prestation',
]);
}
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment