PHP code example of inspheric / nova-defaultable

1. Go to this page and download the library: Download inspheric/nova-defaultable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

inspheric / nova-defaultable example snippets


use Inspheric\NovaDefaultable\HasDefaultableFields;

abstract class Resource extends NovaResource
{
    use HasDefaultableFields;

    // ...
}

use Inspheric\NovaDefaultable\HasDefaultableActionFields;

class YourAction extends Action
{
    use HasDefaultableActionFields;

    // ...
}

Text::make('Name')
    ->default('Default Name'),

Boolean::make('Active')
    ->default(true),

Text::make('Name')
    ->default(function(NovaRequest $request) {
        return $request->user()->name.'\'s New Resource';
    }),

    // $author = $request->user();

    BelongsTo::make('Author')
        ->default($author),
    

    // $id = 1;

    BelongsTo::make('Author')
        ->default($id),
    

    // $post = App\Post::find(1);

    MorphTo::make('Commentable')
        ->types([
            Post::class,
            Video::class,
        ])
        ->default($post),
    

    // $postId = 1;

    MorphTo::make('Commentable')
        ->types(...)
        ->default([$postId, App\Nova\Post::class]), // The Resource class or class name
    

    MorphTo::make('Commentable')
        ->types(...)
        ->default([$postId, App\Post::class]), // The Eloquent model class or class name
    

    MorphTo::make('Commentable')
        ->types(...)
        ->default([$postId, 'posts']), // The uriKey string
    

    // $postResource = Nova::newResourceFromModel(App\Post::find(1));

    MorphTo::make('Commentable')
        ->types(...)
        ->default($postResource),
    

Text::make('Name')
    ->defaultLast(),

BelongsTo::make('Author')
    ->defaultLast(),

class YourAction extends Action
{
    protected $refreshIndex = true;
    
    public function handle(ActionFields $fields, Collection $models)
    {
        // ...
    }
}

$lastInvoiceNumber = auth()->user()->last_invoice_number;

Number::make('Invoice Number')
    ->default($lastInvoiceNumber, function($value, NovaRequest $request) {
        return $value + 1; // Note: Here the $value came from $lastInvoiceNumber
    }),

Number::make('Invoice Number')
    ->defaultLast(function($value, NovaRequest $request) {
        return $value + 1; // Note: Here the $value came from the cache
    }),

BelongsTo::make('Author')
    ->defaultLast(function($value, NovaRequest $request) {
        return $value ?: $request->user()->id;
    }),

use Inspheric\NovaDefault\DefaultableField;

DefaultableField::extend(YourField::class, function($field, $value) {
    return $field->withMeta([
        'value' => $value, // This line is usually 

DefaultableField::extend([YourField::class, YourOtherField::class], function($field, $value) {
    // ...
});

DefaultableField::macro('handleYourField', function($field, $value) {
    // ...
});

DefaultableField::extend(YourField::class, 'handleYourField');
DefaultableField::extend(YourOtherField::class, 'handleYourField');
bash
php artisan vendor:publish --provider="Inspheric\NovaDefaultable\DefaultableServiceProvider" --tag="config"