PHP code example of pkaratanev / nova-repeatable-fields

1. Go to this page and download the library: Download pkaratanev/nova-repeatable-fields 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/ */

    

pkaratanev / nova-repeatable-fields example snippets


namespace App\Nova;

use Pkaratanev\NovaRepeatableFields\Repeater;

// ...

class Petstore extends Resource
{
    // ...

    public function fields(Request $request)
    {
        return [
            // ...

            Repeater::make('Dogs'),

            // ...
        ];
    }
}

namespace App;

// ...

class Petstore extends Model
{
    protected $casts = [
        'dogs' => 'array'
    ]
}


Repeater::make('Dogs')
    ->addField([
        // configuation options
    ])


[
    'label' => 'Dog name',
    //...
]

[
    'name' => 'dog_name',
    //...
]

[
    'type' => 'number',
    //...
]

[
    'placeholder' => 'Name that dog',
    //...
]

[
    'width' => 'w-1/2',
    //...
]

[
    'options' => [
        'fido' => 'Fido',
        'mr_bubbles' => 'Mr Bubbles',
        'preston' => 'Preston'
    ],
    //...
]

[
    'attributes' => [
        'min' => 1,
        'max' => '20',
        'style' => 'color: red'
    ],
    //...
]

Repeater::make('Dogs')
    ->addButtonText('Add new dog');

Repeater::make('Dogs')
    ->summaryLabel('Dogs');

Repeater::make('Dogs')
    ->displayStackedForm();

Repeater::make('Dogs')
    ->initialRows(4);

Repeater::make('Dogs')
    ->maximumRows(4);

Repeater::make('Dogs')
    ->heading('Dog');



namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Schema;

class MigrateRepeaterData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'nfc:migrate';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Migrate your repeater data to be compatible with Nova Flexible Content';


    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // Get the model to run this command for
        $model = $this->ask('Which model do you want to migrate data for?  Enter the full namespace e.g. App\Post');

        if(! class_exists($model)){
            $this->error("Sorry - could not find a model called {$model}");
            return;
        }

        // Get the attribute on the model that holds the old repeater data
        $attribute = $this->ask('Which model attribute holds the data you want to migrate?');

        if(! Schema::hasColumn((new $model)->getTable(), $attribute) ){
            $this->error("Sorry - could not find a database column  called {$attribute} for {$model}");
            return;
        }

        // Get the Nova Flexible Content layout name
        $layout = $this->ask('What is the name of the Nova Flexible Content layout for this data?');

        $models = $model::all();

        if ($this->confirm("About to migrate data for {$models->count()} models.  Do you wish to continue?")) {
            $models->each(function($model) use ($attribute, $layout){
                $model->$attribute = $this->updateValues($model->$attribute, $layout);
                $model->save();
            });

            $this->info('All done!  Please check your data to ensure it was migrated correctly');
        }
    }

    protected function updateValues($data, $layout)
    {
        // Skip anything that is not an array with elements and keep the value the same
        if(! $data){
            return $data;
        }

        return collect($data)
            ->map(function($attributes) use ($layout){
                return [
                    // Create a random key
                    'key' => $this->generateKey(),
                    // my_nova_flexible_content_layout should match the name
                    // you gave to your Nova Flexible Content layout
                    'layout' => $layout,
                    // The data for a given repeater 'row'
                    'attributes' => $attributes
                ];
            })
            ->all();
    }

    protected function generateKey()
    {
        if (function_exists("random_bytes")) {
            $bytes = random_bytes(ceil(16/2));
        }
        elseif (function_exists("openssl_random_pseudo_bytes")) {
            $bytes = openssl_random_pseudo_bytes(ceil(16/2));
        }
        else {
            throw new \Exception("No cryptographically secure random function available");
        }
        return substr(bin2hex($bytes), 0, 16);
    }
}