PHP code example of spykapp / laravel-custom-fields

1. Go to this page and download the library: Download spykapp/laravel-custom-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/ */

    

spykapp / laravel-custom-fields example snippets


return [
    'tables' => [
        'custom_fields' => 'custom_fields',
        'custom_field_responses' => 'custom_field_responses',
    ],
    'field_prefix' => 'cf_', // Custom prefix for custom field attributes
];

use SpykApp\LaravelCustomFields\Traits\HasCustomFields;

class Post extends Model
{
    use HasCustomFields;

    // Your model code here...
}

use App\Models\Post;
use SpykApp\LaravelCustomFields\Models\CustomField;

CustomField::create([
    'name' => 'subtitle',
    'label' => 'Subtitle',
    'field_type' => 'text',
    'default_value' => 'Default Subtitle',
    'model_type' => Post::class,
]);

$post = Post::find(1);
$post->saveCustomFieldResponses([
  [
    'custom_field_id' => 1,
    'value' => 'This is the subtitle',
  ],
  [
    'custom_field_id' => 2,
    // No value provided, so it will use default_value
  ]
], validate: false);

Post::with(['customFieldResponses.customField'])->get();

Post::with(['customFieldResponses.customField'])->find(1);

$post = Post::find(1);
$post->getCustomFieldsWithResponses();

use SpykApp\LaravelCustomFields\Traits\HasCustomFields;
use SpykApp\LaravelCustomFields\Traits\LoadCustomFields;

class Post extends Model
{
    use HasCustomFields, LoadCustomFields;

    // Your model code here...
}

$post = Post::find(1);
echo $post->cf_subtitle; // Outputs the value of the 'subtitle' custom field, or null if not set
bash
php artisan vendor:publish --tag="custom-fields-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="custom-fields-config"