markrassamni/nova-resource-landing-page

Decide if a resource should open on the index, create, update, or detail page

v3.0.0 2020-10-31 05:26 UTC

This package is auto-updated.

Last update: 2024-10-29 05:40:44 UTC


README

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