丹尼尔-德-维特 / nova单一记录资源
Laravel Nova 单一记录资源。在导航栏中直接显示链接到该资源的唯一记录
v1.2.1
2021-02-26 13:14 UTC
Requires
- php: >=7.1.0
README
添加了创建直接到资源详情页的导航链接的能力。对于只包含单个记录的模型非常有用。
先决条件
安装
$ composer require daniel-de-wit/nova-single-record-resource
修改 app/Nova/Resource.php
实现接口 SingleRecordResourceInterface
和特性 SupportSingleRecordNavigationLinks
<?php namespace App\Nova; use DanielDeWit\NovaSingleRecordResource\Contracts\SingleRecordResourceInterface; use DanielDeWit\NovaSingleRecordResource\Traits\SupportSingleRecordNavigationLinks; use Laravel\Nova\Resource as NovaResource; abstract class Resource extends NovaResource implements SingleRecordResourceInterface { use SupportSingleRecordNavigationLinks; ... }
发布资源
$ php artisan vendor:publish --provider="DanielDeWit\NovaSingleRecordResource\Providers\NovaSingleRecordResourceServiceProvider"
更新
在更新时,重要的是要重新发布资源,如下所示
$ php artisan vendor:publish --force --provider="DanielDeWit\NovaSingleRecordResource\Providers\NovaSingleRecordResourceServiceProvider"
卸载
从 composer 中移除
$ composer remove daniel-de-wit/nova-single-record-resource
从您的 Nova 资源中移除 SupportSingleRecordNavigationLinks
特性
use SupportSingleRecordNavigationLinks;
移除自定义的导航模板
rm resources/views/vendor/nova/resources/navigation.blade.php
使用方法
将以下方法放在只有一个记录的模型上。
class MyResource extends Resource { public static function singleRecord(): bool { return true; } }
可选地覆盖资源标识符。
class MyResource extends Resource { /** * @return string|int */ public static function singleRecordId() { return 1; } }
它是如何工作的
Laravel Nova 具有覆盖用于渲染导航侧边栏的 Blade 模板的能力。模板是从 Nova 版本 v1.2.0 复制的,并通过几行代码进行修改以支持直接链接资源到详情视图。当使用标记 nova-views
发布供应商资源时,模板将被放置在项目的 resources/views/vendor/nova/resources
文件夹中。
视图更改
@if ($resource::singleRecord()) <router-link :to="{ name: 'detail', params: { resourceName: '{{ $resource::uriKey() }}', resourceId: {{ $resource::singleRecordId() }} } }" class="text-white text-justify no-underline dim"> {{ $resource::label() }} </router-link> @else <router-link :to="{ name: 'index', params: { resourceName: '{{ $resource::uriKey() }}' } }" class="text-white text-justify no-underline dim"> {{ $resource::label() }} </router-link> @endif