You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
in saved method it only work once I edit the existed page but on create a new page it store an empty array even I attach a directories to a page.
Directory.php
<?php
namespace App\Models;
// ...
class Directory extends Model
{
use HasFactory;
use HasTags;
use Sluggable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'user_id',
'favicon_url',
'logo_url',
'name',
'slug',
'description',
'custom_domain',
'theme',
'is_domain_verified',
'is_ssl_installed',
'is_visible',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'is_domain_verified' => 'boolean',
'is_ssl_installed' => 'boolean',
'is_visible' => 'boolean',
];
}
/**
* 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')->withTimestamps();
}
}
Page.php
<?php
namespace App\Models;
// ...
class Page extends Model
{
use HasFactory;
use HasTags;
use Sluggable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'user_id',
'name',
'slug',
'description',
'links',
'blocks',
'is_visible',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'links' => 'array',
'blocks' => 'array',
'is_visible' => 'boolean',
];
}
/**
* 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')->withTimestamps();
}
/**
* The "booted" method of the model.
*/
protected static function booted(): void
{
static::saved(function (Page $page) {
$links = $page->directories()
->get()
->map(function ($directory) use ($page) {
return "https://{$directory->slug}." . config('app.short_url') . "/{$page->slug}";
})
->toArray();
if ($page->links !== $links) {
$page->links = $links;
$page->saveQuietly();
}
});
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Package
Panel builder
Package Version
v3.2.131
How can we help you?
I want to generate and store URLs for pages based on their associated directories.
https://{directoryName}..app-name.test/{pageSlug}
example:
https://tech-geeks.app-name.test/about-us
and store it in links column.
in saved method it only work once I edit the existed page but on create a new page it store an empty array even I attach a directories to a page.
Directory.php
Page.php
Beta Was this translation helpful? Give feedback.
All reactions