Dec 23, 2024
0
Level 1
Eloquent Many-to-many a link not generated on create only on update
I want to set up the automatic link generation for the many-to-many relationship between Pages and Directories.
Directory.php
<?php
namespace App\Models;
// ...
class Directory extends Model
{
use HasFactory;
use Sluggable;
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'user_id',
'name',
'slug',
];
/**
* Return the sluggable configuration array for this model.
*/
public function sluggable(): array
{
return [
'slug' => [
'source' => 'name',
],
];
}
/**
* Get the user that owns the directory.
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* The pages that belong to the directory.
*/
public function pages(): BelongsToMany
{
return $this->belongsToMany(Page::class, 'directory_page', 'directory_id', 'page_id')->withTimestamps();
}
}
Page.php
<?php
namespace App\Models;
// ...
#[ObservedBy([PageObserver::class])]
class Page extends Model
{
use HasFactory;
use Sluggable;
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'user_id',
'name',
'slug',
'links',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'links' => 'array',
];
}
/**
* Return the sluggable configuration array for this model.
*/
public function sluggable(): array
{
return [
'slug' => [
'source' => 'name',
],
];
}
/**
* Get the user that owns the page.
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* The directories that belong to the page.
*/
public function directories(): BelongsToMany
{
return $this->belongsToMany(Directory::class, 'directory_page', 'page_id', 'directory_id')->withTimestamps();
}
}
PageObserver.php
<?php
namespace App\Observers;
use App\Models\Page;
class PageObserver
{
/**
* Handle the Page "saved" event.
*/
public function saved(Page $page): void
{
$this->generateLinks($page);
}
/**
* Generate and update links for the page.
*/
public function generateLinks(Page $page): void
{
$page->load('directories');
// Generate links based on associated directories.
$links = $page->directories
->map(fn ($directory) => "https://{$directory->slug}." . config('app.short_url') . "/{$page->slug}")
->toArray();
// Update the links field with the generated links.
$page->updateQuietly(['links' => $links]);
}
}
2024_12_20_131705_create_directories_table.php
<?php
use App\Enums\Themes;
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::create('directories', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('name');
$table->string('slug')->index();
$table->timestamps();
$table->softDeletes();
$table->unique(['name', 'slug']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('directories');
}
};
2024_12_20_131750_create_pages_table.php
<?php
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::create('pages', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('name');
$table->string('slug')->index();
$table->json('links')->nullable();
$table->timestamps();
$table->softDeletes();
$table->unique(['name', 'slug']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('pages');
}
};
2024_12_22_140347_create_directory_page_table.php
<?php
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::create('directory_page', function (Blueprint $table) {
$table->id();
$table->foreignId('directory_id')->constrained()->cascadeOnDelete();
$table->foreignId('page_id')->constrained()->cascadeOnDelete();
$table->timestamps();
$table->unique(['directory_id', 'page_id']);
$table->index(['directory_id', 'page_id']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('directory_page');
}
};
Please or to participate in this conversation.