Completed
Push — master ( f9866e...fa91dc )
by Song
02:51
created

src/Auth/Database/Menu.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Encore\Admin\Auth\Database;
4
5
use Encore\Admin\Traits\AdminBuilder;
6
use Encore\Admin\Traits\ModelTree;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9
use Illuminate\Support\Facades\DB;
10
11
/**
12
 * Class Menu.
13
 *
14
 * @property int $id
15
 *
16
 * @method where($parent_id, $id)
17
 */
18
class Menu extends Model
19
{
20
    use AdminBuilder, ModelTree {
21
        ModelTree::boot as treeBoot;
22
    }
23
24
    /**
25
     * The attributes that are mass assignable.
26
     *
27
     * @var array
28
     */
29
    protected $fillable = ['parent_id', 'order', 'title', 'icon', 'uri', 'permission'];
30
31
    /**
32
     * Create a new Eloquent model instance.
33
     *
34
     * @param array $attributes
35
     */
36 View Code Duplication
    public function __construct(array $attributes = [])
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
    {
38
        $connection = config('admin.database.connection') ?: config('database.default');
39
40
        $this->setConnection($connection);
41
42
        $this->setTable(config('admin.database.menu_table'));
43
44
        parent::__construct($attributes);
45
    }
46
47
    /**
48
     * A Menu belongs to many roles.
49
     *
50
     * @return BelongsToMany
51
     */
52
    public function roles() : BelongsToMany
53
    {
54
        $pivotTable = config('admin.database.role_menu_table');
55
56
        $relatedModel = config('admin.database.roles_model');
57
58
        return $this->belongsToMany($relatedModel, $pivotTable, 'menu_id', 'role_id');
59
    }
60
61
    /**
62
     * @return array
63
     */
64
    public function allNodes() : array
65
    {
66
        $connection = config('admin.database.connection') ?: config('database.default');
67
        $orderColumn = DB::connection($connection)->getQueryGrammar()->wrap($this->orderColumn);
68
69
        $byOrder = $orderColumn.' = 0,'.$orderColumn;
70
71
        return static::with('roles')->orderByRaw($byOrder)->get()->toArray();
72
    }
73
74
    /**
75
     * determine if enable menu bind permission.
76
     *
77
     * @return bool
78
     */
79
    public function withPermission()
80
    {
81
        return (bool) config('admin.menu_bind_permission');
82
    }
83
84
    /**
85
     * Detach models from the relationship.
86
     *
87
     * @return void
88
     */
89
    protected static function boot()
90
    {
91
        static::treeBoot();
92
93
        static::deleting(function ($model) {
94
            $model->roles()->detach();
95
        });
96
    }
97
}
98