Skip to content

Polymorphic / Model support #33

@garygreen

Description

@garygreen

In combination with the database support, this will be needed.

It would be cool to add support for models that can implement a method to return menu details/options. This is useful in instances where you have e.g. a pages table and want to build a menu of pages based on their ID, so that if the name or slug updates, it will automatically be updated on the menu.

I'm thinking something along the lines of:

Adding the model menu item for Page ID 4

Menu::create('main', function($menu) {

    $menu->model('App\Page\Page', [
        'id' => 4
    ]);

});

Syntax

$menu->model(object|string $model, array $options)

Menus will call getMenuOptions with the page and should return details about the menu item.

class Page extends Eloquent {

    protected $table = 'pages';
    protected $fillable = [
        'name',
        'slug',
    ];

    /**
     * Get menu options.
     *
     * @param  MenuItem $item
     * @return array
     */
    public static function getMenuOptions(Page $page)
    {
        return [
            'url' => url($page->slug),
            'text' => $page->name,
        ];
    }

}

The class/method that gets called could default to the above but be customised like:

Menu::create('main', function($menu) {

    $menu->model('App\Page\Page', [
        'id' => 4,
        'handler' => 'App\MenuComposers\Page@getOptions'
    ]);

});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions