PHP code example of kenprz / laravel-custom-fields
1. Go to this page and download the library: Download kenprz/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/ */
kenprz / laravel-custom-fields example snippets
class User extends Model
{
use HasCustomFields;
}
// An admin defines a field at runtime.
CustomFields::create([
'model' => User::class,
'key' => 'tax_bracket',
'label' => 'Tax bracket',
'type' => 'select',
'settings' => ['options' => ['a' => 'A', 'b' => 'B']],
]);
// It now works like any other attribute.
$user = User::create(['name' => 'Ken', 'tax_bracket' => 'b']);
$user->tax_bracket; // "b"
$user->update(['tax_bracket' => 'a']);
User::whereCustomField('tax_bracket', 'a')->get();
use Illuminate\Database\Eloquent\Model;
use KenPrz\CustomFields\Traits\HasCustomFields;
class User extends Model
{
use HasCustomFields;
}
$user = User::create(['name' => 'Ken', 'tax_bracket' => 'b']); // no $fillable entry needed
$user->tax_bracket; // "b"
$user->tax_bracket = 'a';
$user->save(); // validated, then saved with the model
$user->update(['tax_bracket' => 'b']);
$user->toArray(); //
class User extends Model
{
use HasCustomFields;
// Which custom fields act as attributes. [] or not set = every field defined for the model.
protected $customFields = ['tax_bracket', 'birthday', 'preferences'];
// Casts for custom field attributes, written exactly like $casts.
protected $customFieldCasts = [
'birthday' => 'immutable_date',
'tax_bracket' => TaxBracket::class, // backed enum
'preferences' => AsCollection::class,
];
// Eager load custom field values on every query, like $with.
protected $loadCustomFields = true;
}
class SaveUserRequest extends FormRequest
{
public function rules(): array
{
$user = $this->route('user') ?? new User;
return [
'name' => ['
use KenPrz\CustomFields\Facades\CustomFields;
$field = CustomFields::create([
'model' => Reservation::class, // age', // => [], // optional: merged over the type's defaults
'validation_rules' => ['min:18'], // optional: extra Laravel rules
'is_
$user->tax_bracket; // cast value, null when not set
$user->listCustomFields(); // ['tax_bracket' => 'b', ...], as stored
$user->getCustomField('birthday'); // "1990-05-01", as stored
$user->getCustomField('missing', 'n/a'); // "n/a"
$user->hasCustomField('tax_bracket'); // true
$users = User::withCustomFields()->get(); // or $loadCustomFields = true on the model
foreach ($users as $user) {
$user->tax_bracket; // no extra queries per record
}
Validator::make($input, (new User)->customFieldRules())->validate();
// Only reservations of $hotel see this field.
CustomFields::forScope($hotel)->create([
'model' => Reservation::class,
'key' => 'vip_reference',
'label' => 'VIP Reference',
'type' => 'text',
]);
use Illuminate\Database\Eloquent\Model;
use KenPrz\CustomFields\Contracts\ScopeResolver;
class CurrentTenant implements ScopeResolver
{
public function resolve(): ?Model
{
return auth()->user()?->team;
}
}
Reservation::whereCustomField('guest_classification', 'vip'); // equals
Reservation::whereCustomField('guest_age', '>=', 18); // = != <> > >= < <=
Reservation::whereCustomFieldIn('guest_classification', ['vip', 'corporate']);
Reservation::whereCustomFieldContains('amenities', 'parking'); // list has the option
Reservation::whereCustomFieldContains('notes', 'late arrival'); // substring, case-insensitive
Reservation::whereCustomFieldNull('notes'); // missing or null
Reservation::whereCustomFieldNotNull('notes');
Reservation::whereCustomField('notes', null); // same as whereCustomFieldNull
use KenPrz\CustomFields\Models\CustomField;
use KenPrz\CustomFields\Types\Type;
class CurrencyFieldType extends Type
{
// Laravel rules for one value. ored (must be JSON encodable). Never receives null.
public function normalize(mixed $value, CustomField $field): mixed
{
return strtoupper($value);
}
// Settings a new field starts with.
public function defaultSettings(): array
{
return [];
}
// Rules for this type's settings, keyed by setting name.
public function settingsRules(): array
{
return [];
}
}
class UserController
{
public function store(SaveUserRequest $request)
{
return User::create($request->validated()); // 201 ->load('customFieldValues');
}
}