PHP code example of crayon / nova-additional-fields

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

    

crayon / nova-additional-fields example snippets


Schema::create('product_fields', function (Blueprint $table) {
    $table->id();
    $table->json('text');
    $table->string('type');
    $table->string('name');
    $table->text('options')->nullable();
    $table->unsignedBigInteger('category_id');
    $table->softDeletes();
    $table->timestamps();

    $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
});

AdditionalFields::make('Additional Fields', 'fields')
    ->parent('category')
    ->endpoint('/api/category/{category}/fields')
    ->hideFromIndex(),
 
Route::get('/category/{category}/fields', [\App\Http\Controllers\Api\NovaHelpers\NovaCategoryController::class, 'fields']);

/**
 * Get available fields by Category ID
 *
 * @param $category
 * @return mixed
 */
public function fields($category)
{
    return Category::findOrFail($category)->fields->map(fn($field) => [
        'id' => $field->id,
        'text' => $field->text,
        'type' => $field->type,
        'name' => $field->name,
        'options' => $field->options
    ]);
}