PHP code example of byrcsc / laravel-custom-fields
1. Go to this page and download the library: Download byrcsc/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/ */
byrcsc / laravel-custom-fields example snippets
use ByRcsc\LaravelCustomFields\Concerns\HasCustomFields;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
use HasCustomFields;
}
use ByRcsc\LaravelCustomFields\Contracts\TenantResolver;
final class SubdomainTenantResolver implements TenantResolver
{
public function currentTenant(): ?string
{
return request()->route('tenant');
}
}
$article->setCustomField('priority', 'high');
$article->setCustomFields(['priority' => 'high', 'reading_minutes' => 9]);
$article->getCustomField('priority');
$article->customFields; // every visible field, keyed by key
final class StoreArticleRequest extends FormRequest
{
public function rules(): array
{
return CustomFields::rulesFor(Article::class, prefix: 'custom_fields');
}
public function attributes(): array
{
return CustomFields::attributeNamesFor(
Article::class,
prefix: 'custom_fields',
);
}
}
use ByRcsc\LaravelCustomFields\Models\CustomFieldValue;
CustomFieldValue::saved(function (CustomFieldValue $value): void {
// reindex the record this value belongs to
});