wtfz / laravel-route-button

从模型生成带有下拉菜单的路由按钮

v1.1.1 2023-01-09 16:39 UTC

This package is auto-updated.

Last update: 2024-09-09 20:18:28 UTC


README

Latest Version on Packagist Total Downloads License

从模型生成带有下拉菜单的路由按钮。

安装

composer require wtfz/laravel-route-button

自定义

要自定义,发布并编辑组件。

php artisan vendor:publish --tag=laravel-route-button
// resources/views/vendor/route-button

使用方法

添加此代码,并在您的模型中定义每个路由按钮。

use Wtfz\LaravelRouteButton\RouteButton;

class YourModel extends Model
{
    use RouteButton;

    // init empty route button
    public static $routeButton = [];

    // or init global route button
    public static $routeButton = [
            [
                'route' => 'admin.auth.user.delete',
                'text' => 'Delete User',
                'view' => 'frontend.profile.confirm',
                'args' => [$this, 1], // Optional, default: $model
            ],
            // ...
        ];

    // or init named route button
    public static $routeButton = [
            'index' => [
                'route' => 'admin.auth.user.edit',
                'text' => 'Edit User'
            ],
            'edit' => [
                'route' => 'admin.auth.user.destroy',
                'text' => 'Destroy User'
            ],
            // ...
        ];

    // or init mixed route button
    public static $routeButton = [
            [
                'route' => 'admin.auth.user.edit',
                'text' => 'Edit User',
                'args' => [$this, 1], // Optional, default: $model
            ],
            'index' => [
                'route' => 'admin.auth.user.edit',
                'text' => 'Edit User'
            ],
            'edit' => [
                'route' => 'admin.auth.user.destroy',
                'text' => 'Destroy User'
            ],
            // ...
        ];
}

通过从模型调用它,在您的视图中渲染路由按钮。

// global route button
{{ $model->routeButton() }}

// named route button
{{ $model->routeButton('edit') }}

// or in Livewire table...
Column::make(__('Actions'), 'id')
    ->format(function ($value, $row, $column) {
        return $row->routeButton('edit');
    }),

// or add new route button on the fly...
Column::make(__('Actions'), 'id')
    ->format(function ($value, $row, $column) {
        // for global route (set integer index)
        $row::$routeButton[0][] = [
            'route' => 'admin.auth.user.delete',
            'text' => 'Delete User',
            'args' => ['type' => 'member', 'user' => $row],
        ];

        // for named route (set string index)
        $row::$routeButton['edit'][] = [
            'route' => 'admin.auth.user.delete',
            'text' => 'Delete User',
            'args' => ['type' => 'member', 'user' => $row],
        ];

        return $row->routeButton('index');
    }),

许可证

版权 © Ahmad Zaim

MIT许可证 (MIT)