Skip to content

Commit

Permalink
Settings without group
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvenga committed Dec 11, 2022
1 parent 0232ec2 commit ac0d153
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ public function up()
Schema::create('setting_groups', function (Blueprint $table) {
$table->id();
$table->string('slug', 190)->unique();
$table->string('name', 190)->nullable();
$table->string('hint', 190)->nullable();
$table->timestamps();
});

\VI\LaravelSiteSettings\Models\SettingGroup::create(['slug' =>'default', 'name' =>'Default']);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ public function up()
$table->id();

$table->foreignId('setting_group_id')
->nullable()
->constrained()
->cascadeOnDelete()
->cascadeOnUpdate();

$table->string('slug', 190);
$table->string('name', 190)->nullable();
$table->string('hint', 190)->nullable();
$table->string('value', 190);
$table->timestamps();

Expand Down
8 changes: 7 additions & 1 deletion src/LaravelSiteSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\Config\Repository as ConfigContract;
use Illuminate\Support\Arr;
use VI\LaravelSiteSettings\Models\Setting;
use VI\LaravelSiteSettings\Models\SettingGroup;

class LaravelSiteSettings implements ConfigContract
Expand All @@ -15,13 +16,18 @@ public function __construct()
{

$this->items = cache()->rememberForever(config('laravelsitesettings.cache_key'), function () {
return SettingGroup::all()
$withGroup = SettingGroup::all()
->load('settings')
->keyBy('slug')
->map(function ($group) {
return $group->settings->pluck('value', 'slug');
})
->toArray();
$withoutGroup = Setting::whereNull('setting_group_id')->get()
->pluck('value', 'slug')
->toArray();

return array_merge($withGroup, $withoutGroup);
});

}
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Setting extends Model
protected $fillable = [
'setting_group_id',
'slug',
'name',
'hint',
'value',
];

Expand Down
2 changes: 1 addition & 1 deletion src/Models/SettingGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class SettingGroup extends Model

protected $fillable = [
'slug',
'name',
'hint',
];

public function settings(): HasMany
Expand Down
6 changes: 3 additions & 3 deletions stubs/MoonShine/Resources/SettingGroupResource.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SettingGroupResource extends Resource
return [
ID::make()->sortable(),
Text::make('Slug', 'slug')->required()->sortable()->hint('a-z, 0-9, -'),
Text::make('Hint', 'name')->nullable()->sortable()->hint('Не используется на сайте, только для удобства администрирования!'),
Text::make('Hint', 'hint')->nullable()->sortable()->hint('Не используется на сайте, только для удобства администрирования!'),
Date::make('Updated at', 'updated_at')->sortable()->hideOnForm(),
];
}
Expand All @@ -33,13 +33,13 @@ class SettingGroupResource extends Resource
{
return [
'slug' => 'required|max:190|regex:/^([a-z0-9\-]+)$/i|unique:laravel_site_setting_groups,slug,' . $item->getKey(),
'name' => 'nullable|max:190',
'hint' => 'nullable|max:190',
];
}

public function search(): array
{
return ['id', 'slug', 'name'];
return ['id', 'slug', 'hint'];
}

public function filters(): array
Expand Down
14 changes: 6 additions & 8 deletions stubs/MoonShine/Resources/SettingResource.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ class SettingResource extends Resource
ID::make()->sortable(),
BelongsTo::make(
'Group',
'setting_group_id',
fn($item) => $item->name_human
'setting_group_id'
)->nullable()->sortable(),
Text::make('Slug', 'slug')->required()->sortable()->hint('a-z, 0-9, -'),
Text::make('Hint', 'name')->nullable()->sortable()->hint('Не используется на сайте, только для удобства администрирования!'),
Text::make('Hint', 'hint')->nullable()->sortable()->hint('Не используется на сайте, только для удобства администрирования!'),
Text::make('Value', 'value')->required()->sortable(),
Date::make('Updated at', 'updated_at')->sortable()->hideOnForm(),
];
Expand All @@ -39,25 +38,24 @@ class SettingResource extends Resource
public function rules(Model $item): array
{
return [
'setting_group_id' => 'required|exists:setting_groups,id',
'setting_group_id' => 'nullable|exists:setting_groups,id',
'slug' => 'required|max:190|regex:/^([a-z0-9\-]+)$/i|unique:laravel_site_setting_groups,slug,' . $item->getKey(),
'name' => 'nullable|max:190',
'hint' => 'nullable|max:190',
'value' => 'required|max:190',
];
}

public function search(): array
{
return ['id', 'slug', 'name', 'value'];
return ['id', 'slug', 'hint', 'value'];
}

public function filters(): array
{
return [
BelongsTo::make(
'Group',
'setting_group_id',
fn($item) => $item->name_human
'setting_group_id'
)->nullable(),
];
}
Expand Down

0 comments on commit ac0d153

Please sign in to comment.