Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear menu-cache when deleting items #4083

Merged
merged 5 commits into from May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/Models/Menu.php
Expand Up @@ -16,6 +16,19 @@ class Menu extends Model

protected $guarded = [];

public static function boot()
{
parent::boot();

static::saved(function ($model) {
$model->removeMenuFromCache();
});

static::deleted(function ($model) {
$model->removeMenuFromCache();
});
}

public function items()
{
return $this->hasMany(Voyager::modelClass('MenuItem'));
Expand Down Expand Up @@ -86,12 +99,9 @@ public static function display($menuName, $type = null, array $options = [])
);
}

public function save(array $options = [])
public function removeMenuFromCache()
{
//Remove from cache
\Cache::forget('voyager_menu_'.$this->name);

parent::save();
}

private static function processItems($items)
Expand Down
25 changes: 17 additions & 8 deletions src/Models/MenuItem.php
Expand Up @@ -21,6 +21,23 @@ class MenuItem extends Model

protected $translatable = ['title'];

public static function boot()
{
parent::boot();

static::created(function ($model) {
$model->menu->removeMenuFromCache();
});

static::saved(function ($model) {
$model->menu->removeMenuFromCache();
});

static::deleted(function ($model) {
$model->menu->removeMenuFromCache();
});
}

public function children()
{
return $this->hasMany(Voyager::modelClass('MenuItem'), 'parent_id')
Expand Down Expand Up @@ -115,12 +132,4 @@ public function highestOrderMenuItem($parent = null)

return $order;
}

public function save(array $options = [])
{
//Remove from cache
\Cache::forget('voyager_menu_'.$this->menu->name);

parent::save();
}
}