PHP code example of lopatin96 / laravel-configurator
1. Go to this page and download the library: Download lopatin96/laravel-configurator 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/ */
lopatin96 / laravel-configurator example snippets
use Atin\LaravelConfigurator\Traits\HasConfig;
class User extends Authenticatable
{
use HasConfig, …
protected $casts = [
…
'config' => 'object',
];
namespace App\Enums;
enum ConfigKey: string
{
case TestKey = 'TestKey';
case TestKeyPro = 'TestKeyPro';
}
namespace App\Nova;
use App\Enums\ConfigCategory;
use Atin\LaravelConfigurator\Enums\ConfigType;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\Number;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Fields\Textarea;
use Laravel\Nova\Http\Requests\NovaRequest;
use function PHPUnit\Framework\matches;
class Config extends Resource
{
public static string $model = \Atin\LaravelConfigurator\Models\Config::class;
public static $title = 'key';
public static $search = [
'key',
'title',
'value',
'description',
];
public function fields(NovaRequest $request): array
{
return [
Text::make('Key')->sortable()->readonly(),
Select::make('Category')->options([
ConfigCategory::Profiles->value => ConfigCategory::Profiles->value,
])
->sortable()
->readonly(),
Text::make('Title')
->rules('nullable', 'max:64')
->sortable()
->hideFromIndex(),
match ($this->type) {
ConfigType::ArrayOfStrings => Text::make('Value')->displayUsing(fn () => mb_strimwidth($this->value, 0, 50, '…'))->onlyOnIndex(),
default => Textarea::make('Value')->hide()->hideFromDetail(),
},
match ($this->type) {
ConfigType::Integer => Number::make('Value'),
ConfigType::Float => Number::make('Value')->step(0.01),
ConfigType::Boolean => Boolean::make('Value'),
ConfigType::ArrayOfStrings => Textarea::make('Value')->alwaysShow(),
default => Text::make('Value')->displayUsing(fn () => mb_strimwidth($this->value, 0, 50, '…')),
},
Select::make('Type')->options([
ConfigType::String->value => ConfigType::String->value,
ConfigType::Integer->value => ConfigType::Integer->value,
ConfigType::Float->value => ConfigType::Float->value,
ConfigType::Boolean->value => ConfigType::Boolean->value,
ConfigType::ArrayOfStrings->value => ConfigType::ArrayOfStrings->value,
ConfigType::ArrayOfIntegers->value => ConfigType::ArrayOfIntegers->value,
])
->sortable(),
Text::make('Description')
->rules('nullable', 'max:256')
->hideFromIndex(),
];
}
public static function authorizedToCreate(Request $request): bool
{
return false;
}
public function authorizedToDelete(Request $request): bool
{
return false;
}
}
class User extends Resource
{
…
public function fields(NovaRequest $request): array
{
return [
ID::make()
->sortable()
->hideFromIndex(),
new Panel('Config', $this->configFields()),
…
}
protected function configFields(): array
{
return [
KeyValue::make('Config')->rules('json'),
];
}
…
}
use App\Enums\ConfigKey;
use Atin\LaravelConfigurator\Helpers\ConfiguratorHelper;
class DashboardController extends Controller
{
public function index()
{
$value = ConfiguratorHelper::getLimitedValue(ConfigKey::TestKey);
…
}
}