nova-kit/nova-field-mixins

Laravel Nova Field Mixins

v2.2.0 2024-04-23 00:35 UTC

This package is auto-updated.

Last update: 2024-11-16 03:32:17 UTC


README

tests Latest Stable Version Total Downloads Latest Unstable Version License Coverage Status

Installation

To install through composer, run the following command from the terminal:

composer require "nova-kit/nova-field-mixins"

Usages

Laravel Nova Field Mixins are useful for applying a common set of configurations to a Field without repeating it. E.g:

use Laravel\Nova\Fields\DateTime;

DateTime::make('Created At')->sortable()->displayUsing(fn ($d) => $d?->diffForHumans()),
DateTime::make('Updated At')->sortable()->displayUsing(fn ($d) => $d?->diffForHumans()),

By adding the following class App\Nova\Fields\Mixins\StandardDateTime:

<?php 

namespace App\Nova\Fields\Mixins;

use Laravel\Nova\Fields\Field;

class StandardDateTime
{
    public function __invoke(Field $field)
    {
        $field->sortable()->displayUsing(fn ($d) => $d?->diffForHumans());
    }
}

You can now write the above example as:

use App\Nova\Fields\Mixins\StandardDateTime;
use Laravel\Nova\Fields\DateTime;

DateTime::make('Created At')->apply(StandardDateTime::class),
DateTime::make('Updated At')->apply(StandardDateTime::class),

Available Mixin

Handle AsArrayobject Cast

This package provides a default implement casting for AsArrayObject.

use Laravel\Nova\Fields\Text;
use NovaKit\Fields\Mixins\AsArrayObject;

Text::make('Name', 'profile.name')->apply(new AsArrayObject()),

You can also simplify this by using fromArrayObject macro.

use Laravel\Nova\Fields\Text;

Text::make('Name', 'profile.name')->fromArrayObject(),