PHP code example of plin-code / laravel-custom-fields
1. Go to this page and download the library: Download plin-code/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/ */
plin-code / laravel-custom-fields example snippets
use PlinCode\CustomFields\Facades\CustomFields;
$fieldModel = CustomFields::fieldModel();
$valueModel = CustomFields::valueModel();
declare(strict_types=1);
namespace App\Providers;
use App\Models\Patient;
use Illuminate\Support\ServiceProvider;
use PlinCode\CustomFields\Facades\CustomFields;
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
CustomFields::registerEntity(Patient::class, 'patient', 'Patient');
}
}
declare(strict_types=1);
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use PlinCode\CustomFields\Concerns\HasCustomFields;
class Patient extends Model
{
use HasCustomFields;
}
$patient->setCustomField('date-of-birth', null); // the row is deleted
$patient->getCustomField('date-of-birth'); // null
$patient->clearCustomField('date-of-birth'); // the same thing, explicitly
$patient->setCustomField('allergies', []); // row kept, reads back as []
$patient->setCustomField('allergies', null); // row deleted, reads back as []
$riskLevel->optionsForInput(); // the active options, ready for a form
$riskLevel->activeOptionKeys(); // ['low', 'high', 'critical']
$riskLevel->optionKeys(); // ['low', 'high', 'legacy', 'critical']
use App\Models\Patient;
use PlinCode\CustomFields\Facades\CustomFields;
use Spatie\QueryBuilder\QueryBuilder;
public function index()
{
$options = CustomFields::queryOptionsFor(Patient::class);
return QueryBuilder::for(Patient::class)
->allowedFilters(...$options['filters'])
->allowedSorts(...$options['sorts'])
->paginate();
}
use App\Models\Patient;
use PlinCode\CustomFields\Facades\CustomFields;
use Spatie\QueryBuilder\QueryBuilder;
$patients = QueryBuilder::for(Patient::class)
->allowedFilters(...CustomFields::filtersFor(Patient::class))
->allowedSorts(...CustomFields::sortsFor(Patient::class))
->paginate();
use App\Models\Patient;
use PlinCode\CustomFields\Facades\CustomFields;
use PlinCode\EloquentSorts\Sorts\RelationSorter;
use Spatie\QueryBuilder\AllowedSort;
use Spatie\QueryBuilder\QueryBuilder;
$options = CustomFields::queryOptionsFor(Patient::class);
$patients = QueryBuilder::for(Patient::class)
->allowedFilters(...$options['filters'])
->allowedSorts(
AllowedSort::field('last_name'),
AllowedSort::custom('clinic', new RelationSorter('clinics', 'clinic_id')),
...$options['sorts'],
)
->paginate();
declare(strict_types=1);
namespace App\CustomFields;
use Illuminate\Database\Eloquent\Model;
use PlinCode\CustomFields\Types\TextType;
class CountryType extends TextType
{
public static function key(): string
{
return 'country';
}
public function rules(Model $field): array
{
return ['nullable', 'string', 'size:2'];
}
public function label(): string
{
return 'Country';
}
public function queryOperations(): array
{
return ['equals', 'in', 'is_null', 'is_not_null', 'sort'];
}
}