Skip to content

Commit

Permalink
Added Laravel Category Trait
Browse files Browse the repository at this point in the history
  • Loading branch information
balajidharma committed Nov 7, 2024
1 parent e8cdff4 commit 3835414
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
6 changes: 2 additions & 4 deletions src/Models/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

namespace BalajiDharma\LaravelMenu\Models;

use BalajiDharma\LaravelMenu\Traits\LaravelCategories;
use BalajiDharma\LaravelMenu\Traits\MenuTree;
use BalajiDharma\LaravelMenu\Traits\SpatiePermission;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class MenuItem extends Model
{
use MenuTree {
MenuTree::boot as treeBoot;
}
use SpatiePermission;
use LaravelCategories, MenuTree, SpatiePermission;

/**
* The attributes that aren't mass assignable.
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/LaravelCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace BalajiDharma\LaravelMenu\Traits;

if (class_exists(\Spatie\Permission\PermissionRegistrar::class)) {
if (class_exists(\BalajiDharma\LaravelCategory\CategoryServiceProvider::class)) {
trait LaravelCategories
{
use \BalajiDharma\LaravelCategory\Traits\HasCategories;
Expand Down
32 changes: 14 additions & 18 deletions src/Traits/MenuTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ trait MenuTree
*/
protected $queryCallback;

/**
* {@inheritdoc}
*/
protected static function bootMenuTree()
{
static::saving(function (Model $branch) {
$parentColumn = $branch->getParentColumn();

if (Request::filled($parentColumn) && Request::input($parentColumn) == $branch->getKey()) {
throw InvalidParent::create();
}
});
}

/**
* Get children of current node.
*
Expand Down Expand Up @@ -334,24 +348,6 @@ public function initializeMenuTree()
$this->appends = array_unique(array_merge($this->appends, ['link']));
}

/**
* {@inheritdoc}
*/
protected static function boot()
{
parent::boot();

static::saving(function (Model $branch) {
$parentColumn = $branch->getParentColumn();

if (Request::filled($parentColumn) && Request::input($parentColumn) == $branch->getKey()) {
throw InvalidParent::create();
}

return $branch;
});
}

protected function checkHasPermission($menuItem)
{
if (! $this->hasSpatiePermission) {
Expand Down

0 comments on commit 3835414

Please sign in to comment.