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

markrassamni/nova-resource-landing-page

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Nova: Resource Landing Page

License Latest Version on Packagist Total Downloads

Adds the ability to create a navigation link directly to the detail, create, or edit page of a resource.

Prerequisites

Installation

$ composer require markrassamni/nova-resource-landing-page

Modify app/Nova/Resource.php to implement ResourceLandingPageInterface and the SupportChangeResourceLandingPage trait:

<?php

namespace App\Nova;

use MarkRassamni\NovaResourceLandingPage\Traits\SupportChangeResourceLandingPage;
use MarkRassamni\NovaResourceLandingPage\Contracts\ResourceLandingPageInterface;
use Laravel\Nova\Resource as NovaResource;

abstract class Resource extends NovaResource implements ResourceLandingPageInterface
{
    use SupportChangeResourceLandingPage;

    ...
}

Publish assets:

$ php artisan vendor:publish --provider="MarkRassamni\NovaResourceLandingPage\Providers\NovaResourceLandingPageServiceProvider"

Update

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

$ php artisan vendor:publish --force --provider="MarkRassamni\NovaResourceLandingPage\Providers\NovaResourceLandingPageServiceProvider"

Uninstallation

Remove from composer

$ composer remove markrassamni/nova-resource-landing-page

Remove SupportChangeResourceLandingPage trait from your Nova Resources

use SupportChangeResourceLandingPage;

Remove the customized navigation template

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

Usage

Place the following method on models that should land on the detail page.

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

Place the following method on models that should land on the create page.

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

Place the following method on models that should land on the edit page.

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

Optionally override the resource identifier when opening the detail or edit page.

class MyResource extends Resource
{
    /**
     * @return string|int
     */
    public static function recordId()
    {
        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 v3.4.1 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::detail())
    <router-link :to="{
        name: 'detail',
        params: {
            resourceName: '{{ $resource::uriKey() }}',
            resourceId: {{ $resource::recordId() }}
        }
    }" class="text-white text-justify no-underline dim">
        {{ $resource::label() }}
    </router-link>
@elseif ($resource::create())
    <router-link :to="{
        name: 'create',
        params: {
            resourceName: '{{ $resource::uriKey() }}',
        }
    }" class="text-white text-justify no-underline dim">
        {{ $resource::label() }}
    </router-link>
@elseif ($resource::edit())
    <router-link :to="{
        name: 'edit',
        params: {
            resourceName: '{{ $resource::uriKey() }}',
            resourceId: {{ $resource::recordId() }}
        }
    }" 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 72.4%
  • PHP 27.6%