Skip to content

Commit 8f92d43

Browse files
committed
New resources for MoonShine
1 parent c98242d commit 8f92d43

File tree

6 files changed

+243
-168
lines changed

6 files changed

+243
-168
lines changed

README.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,21 @@ settings(['setting' => 'Value']);
8383

8484
Please see [MoonShine](https://moonshine.cutcode.ru/)
8585

86-
You can publish two [MoonShine Resources](https://moonshine.cutcode.ru/resources-index) with command:
87-
88-
```bash
89-
php artisan vendor:publish --provider="VI\LaravelSiteSettings\LaravelSiteSettingsProvider" --tag="moonshine"
90-
```
91-
92-
and use them in your MoonShine admin panel, like this:
86+
You can use settings in your MoonShine admin panel, like this:
9387

9488
```php
9589
MenuGroup::make('Settings', [
96-
MenuItem::make('Settings', SettingResource::class)->icon('app'),
97-
MenuItem::make('Settings groups', SettingGroupResource::class)->icon('app'),
98-
])->icon('app'),
90+
MenuItem::make(
91+
'Setting groups',
92+
new \VI\LaravelSiteSettings\MoonShine\Resources\SettingGroupResource(),
93+
'heroicons.outline.wrench-screwdriver'
94+
),
95+
MenuItem::make(
96+
'Settings',
97+
new \VI\LaravelSiteSettings\MoonShine\Resources\SettingResource(),
98+
'heroicons.outline.wrench'
99+
),
100+
], 'heroicons.outline.cog-8-tooth'),
99101
```
100102

101103
## Seeding settings

src/LaravelSiteSettingsProvider.php

-5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ public function boot()
3030
__DIR__ . '/../config/config.php' => config_path('laravelsitesettings.php'),
3131
], 'config');
3232

33-
$this->publishes([
34-
__DIR__ . '/../stubs/MoonShine/Resources/SettingGroupResource.php.stub' => app_path('MoonShine/Resources/SettingGroupResource.php'),
35-
__DIR__ . '/../stubs/MoonShine/Resources/SettingResource.php.stub' => app_path('MoonShine/Resources/SettingResource.php'),
36-
], 'moonshine');
37-
3833
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
3934

4035
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
namespace VI\LaravelSiteSettings\MoonShine\Resources;
4+
5+
use Illuminate\Contracts\Database\Eloquent\Builder;
6+
use Leeto\MoonShine\Actions\ExportAction;
7+
use Leeto\MoonShine\Decorations\Block;
8+
use Leeto\MoonShine\Fields\Date;
9+
use Leeto\MoonShine\Fields\ID;
10+
use Leeto\MoonShine\Fields\NoInput;
11+
use Leeto\MoonShine\Fields\Text;
12+
use Leeto\MoonShine\Resources\Resource;
13+
use VI\LaravelSiteSettings\Models\SettingGroup;
14+
15+
16+
class SettingGroupResource extends Resource
17+
{
18+
public static string $model = SettingGroup::class;
19+
20+
public static string $title = 'Settings groups';
21+
22+
public string $titleField = 'slug';
23+
24+
// TODO Add translation
25+
//public function title(): string
26+
//{
27+
// return trans('moonshine::ui.resource.admins_title');
28+
//}
29+
30+
public function fields(): array
31+
{
32+
return [
33+
Block::make('Main', [
34+
ID::make()->sortable(),
35+
36+
Text::make('Slug', 'slug')
37+
->required()
38+
->sortable()
39+
->hint('a-z, 0-9, -, _')
40+
->showOnExport(),
41+
42+
Text::make('Hint', 'hint')
43+
->nullable()
44+
->sortable()
45+
->hint('Не используется на сайте, только для удобства администрирования!')
46+
->showOnExport(),
47+
48+
NoInput::make('Created at', 'created_at',
49+
fn(SettingGroup $item)=>$item->created_at->isoFormat('lll'))
50+
->sortable()
51+
->hideOnForm()
52+
->showOnExport(),
53+
54+
NoInput::make('Updated at', 'updated_at',
55+
fn(SettingGroup $item)=>$item->updated_at->isoFormat('lll'))
56+
->sortable()
57+
->hideOnForm()
58+
->showOnExport(),
59+
60+
NoInput::make('Count settings', 'settings_count')
61+
->sortable()
62+
->hideOnForm()
63+
->hideOnDetail()
64+
->showOnExport(),
65+
]),
66+
];
67+
}
68+
69+
public function components(): array
70+
{
71+
return [
72+
];
73+
}
74+
75+
public function rules($item): array
76+
{
77+
return [
78+
'slug' => 'required|max:190|regex:/^([a-z0-9\-\_]+)$/i|unique:setting_groups,slug,' . $item->getKey(),
79+
'hint' => 'nullable|max:190',
80+
];
81+
}
82+
83+
public function search(): array
84+
{
85+
return ['id', 'slug', 'hint'];
86+
}
87+
88+
public function filters(): array
89+
{
90+
return [
91+
];
92+
}
93+
94+
public function actions(): array
95+
{
96+
return [
97+
ExportAction::make(trans('moonshine::ui.export')),
98+
];
99+
}
100+
101+
public function query(): Builder
102+
{
103+
return parent::query()->withCount('settings');
104+
}
105+
}
+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
namespace VI\LaravelSiteSettings\MoonShine\Resources;
4+
5+
use Illuminate\Contracts\Database\Eloquent\Builder;
6+
use Illuminate\Validation\Rule;
7+
use Leeto\MoonShine\Actions\ExportAction;
8+
use Leeto\MoonShine\Decorations\Block;
9+
use Leeto\MoonShine\Fields\BelongsTo;
10+
use Leeto\MoonShine\Fields\ID;
11+
use Leeto\MoonShine\Fields\NoInput;
12+
use Leeto\MoonShine\Fields\Text;
13+
use Leeto\MoonShine\Fields\Textarea;
14+
use Leeto\MoonShine\Resources\Resource;
15+
use VI\LaravelSiteSettings\Models\Setting;
16+
17+
18+
class SettingResource extends Resource
19+
{
20+
public static string $model = Setting::class;
21+
22+
public static string $title = 'Settings';
23+
24+
public string $titleField = 'slug';
25+
26+
// TODO Add translation
27+
//public function title(): string
28+
//{
29+
// return trans('moonshine::ui.resource.admins_title');
30+
//}
31+
32+
public function fields(): array
33+
{
34+
return [
35+
Block::make('Main', [
36+
ID::make()->sortable(),
37+
38+
BelongsTo::make(
39+
'Group',
40+
'settingGroup',
41+
new SettingGroupResource()
42+
)
43+
->nullable()
44+
->sortable()
45+
->showOnExport(),
46+
47+
Text::make('Slug', 'slug')
48+
->required()
49+
->sortable()
50+
->hint('a-z, 0-9, -, _')
51+
->showOnExport(),
52+
53+
Text::make('Hint', 'hint')
54+
->nullable()
55+
->sortable()
56+
->hint('Не используется на сайте, только для удобства администрирования!')
57+
->showOnExport(),
58+
59+
Textarea::make('Value', 'value')->nullable()->sortable()
60+
->showOnExport(),
61+
62+
NoInput::make('Created at', 'created_at',
63+
fn(Setting $item) => $item->created_at->isoFormat('lll'))
64+
->sortable()
65+
->hideOnForm()
66+
->showOnExport(),
67+
68+
NoInput::make('Updated at', 'updated_at',
69+
fn(Setting $item) => $item->updated_at->isoFormat('lll'))
70+
->sortable()
71+
->hideOnForm()
72+
->showOnExport(),
73+
74+
75+
]),
76+
];
77+
}
78+
79+
public function components(): array
80+
{
81+
return [
82+
];
83+
}
84+
85+
public function rules($item): array
86+
{
87+
return [
88+
'setting_group_id' => 'nullable|exists:setting_groups,id',
89+
'slug' => [
90+
'required',
91+
'max:190',
92+
'regex:/^([a-z0-9\-\_]+)$/i',
93+
'unique:setting_groups,slug',
94+
Rule::unique('settings', 'slug')->where(function ($query) use ($item) {
95+
return $query->where('id', '!=', $item->getKey())
96+
->where('setting_group_id', $item->setting_group_id);
97+
}),
98+
],
99+
'hint' => 'nullable|max:190',
100+
'value' => 'nullable|string',
101+
];
102+
}
103+
104+
public function search(): array
105+
{
106+
return ['id', 'slug', 'hint', 'value'];
107+
}
108+
109+
public function filters(): array
110+
{
111+
return [
112+
];
113+
}
114+
115+
public function actions(): array
116+
{
117+
return [
118+
ExportAction::make(trans('moonshine::ui.export')),
119+
];
120+
}
121+
122+
public function query(): Builder
123+
{
124+
return parent::query()->with('settingGroup');
125+
}
126+
}

stubs/MoonShine/Resources/SettingGroupResource.php.stub

-59
This file was deleted.

0 commit comments

Comments
 (0)