Skip to content

Shows a link in the navigation bar directly to the only record of the resource

Notifications You must be signed in to change notification settings

mvdstam/nova-single-record-resource

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Nova: Single Record Resource

License Latest Version on Packagist Total Downloads StyleCI

Adds the ability to create a navigation link directly to the detail page of a resource. Useful for models that will contain only a single record.

Prerequisites

Installation

$ composer require daniel-de-wit/nova-single-record-resource

Modify app/Nova/Resource.php to implement SingleRecordResourceInterface and the SupportSingleRecordNavigationLinks trait:

<?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;

    ...
}

Publish assets:

$ php artisan vendor:publish --provider="DanielDeWit\NovaSingleRecordResource\Providers\NovaSingleRecordResourceServiceProvider"

Update

When updating it is important to republish the assets, like so:

$ php artisan vendor:publish --force --provider="DanielDeWit\NovaSingleRecordResource\Providers\NovaSingleRecordResourceServiceProvider"

Uninstallation

Remove from composer

$ composer remove daniel-de-wit/nova-single-record-resource

Remove SupportSingleRecordNavigationLinks trait from your Nova Resources

use SupportSingleRecordNavigationLinks;

Remove the customized navigation template

rm resources/views/vendor/nova/resources/navigation.blade.php

Usage

Place the following method on models that have only a single record.

class MyResource extends Resource
{
    public static function singleRecord(): bool
    {
        return true;
    }
}

Optionally override the resource identifier.

class MyResource extends Resource
{
    /**
     * @return string|int
     */
    public static function singleRecordId()
    {
        return 1;
    }
}

How it works

Laravel Nova has the ability to override the Blade template used to render the navigation sidebar. The template is copied from Nova version v1.2.0 and altered with a few lines to support linking a resource directly to the detail view. When publishing vendor assets with the tag nova-views the template will be placed in the project resources/views/vendor/nova/resources folder.

View changes
@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

About

Shows a link in the navigation bar directly to the only record of the resource

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Blade 61.3%
  • PHP 38.7%