novius/laravel-nova-contexts

A Laravel Nova tool to manage resources's contexts.

1.0.0 2021-01-06 10:36 UTC

This package is not auto-updated.

Last update: 2021-10-14 13:34:05 UTC


README

This package allows you to manage resource contexts.

It provides :

  • Default context choice during back-office navigation (on nova header).
  • Context filter bases on default context.
  • Context field for resources.

Requirements

  • PHP >= 7.4
  • Laravel Framework >= 7.0

Installation

composer require novius/laravel-nova-contexts

Add LaravelNovaContexts tool to NovaServiceProvider:

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Laravel\Nova\Cards\Help;
use Laravel\Nova\Nova;
use Laravel\Nova\NovaApplicationServiceProvider;
use Novius\LaravelNovaContexts\LaravelNovaContexts;

class NovaServiceProvider extends NovaApplicationServiceProvider
{
    // ...
    
    public function tools()
    {
        return [
            new LaravelNovaContexts(),
        ];
    }
}

Override Nova Layout view to add context selector like bellow :

{{-- resources/views/vendor/nova/layout.blade.php --}}

@if (count(\Laravel\Nova\Nova::globallySearchableResources(request())) > 0)
    <global-search dusk="global-search-component"></global-search>
@endif

<laravel-nova-context-selector></laravel-nova-context-selector>

<dropdown class="ml-auto h-9 flex items-center dropdown-right">
    @include('nova::partials.user')
</dropdown>

Configuration

Some options that you can override are available.

php artisan vendor:publish --provider="Novius\LaravelNovaContexts\LaravelNovaContextsServiceProvider" --tag="config"

Models configuration

To make a model "contextable", just use HasContext trait.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Novius\LaravelNovaContexts\Traits\HasContext;

class Page extends Model
{
    use HasContext;

    /**
     * @return string
     */
    public function contextFieldName(): string
    {
        return 'locale';
    }
}

Nova resource field configuration

You can add context field to your Nova resource's fields:

public function fields(Request $request)
{
    return [
        // ...
        
        ContextField::make('locale'),
        
        // ...
    ];
}

Nova resource filter configuration

You can add context filter to your Nova resource's filters :

/**
 * Get the filters available for the resource.
 *
 * @param \Illuminate\Http\Request $request
 * @return array
 */
public function filters(Request $request)
{
    return [
        new ContextFilter($this->model()),
    ];
}

Lint

Run php-cs with:

composer run-script lint

Contributing

Contributions are welcome!

Leave an issue on Github, or create a Pull Request.

Licence

This package is under GNU Affero General Public License v3 or (at your option) any later version.