-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathArticleResource.php
More file actions
129 lines (113 loc) · 4.35 KB
/
ArticleResource.php
File metadata and controls
129 lines (113 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace App\Filament\Resources;
use App\Filament\Resources\ArticleResource\Actions\PreviewAction;
use App\Filament\Resources\ArticleResource\Actions\PublishAction;
use App\Filament\Resources\ArticleResource\Actions\UnpublishAction;
use App\Filament\Resources\ArticleResource\Pages;
use App\Models\Article;
use Filament\Actions;
use Filament\Actions\ActionGroup;
use Filament\Forms\Components\MarkdownEditor;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Set;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Support\Str;
class ArticleResource extends Resource
{
protected static ?string $model = Article::class;
protected static ?string $recordRouteKeyName = 'id';
protected static ?string $recordTitleAttribute = 'title';
protected static \BackedEnum|string|null $navigationIcon = 'heroicon-o-newspaper';
public static function form(Schema $schema): Schema
{
return $schema
->schema([
TextInput::make('title')
->required()
->maxLength(255)
->live(onBlur: true)
->afterStateUpdated(function (Article $article, Set $set, ?string $state): void {
if ($article->isPublished()) {
return;
}
$set('slug', Str::slug($state));
}),
TextInput::make('slug')
->required()
->maxLength(255)
->live(onBlur: true)
->unique(Article::class, 'slug', ignoreRecord: true)
->disabled(fn (Article $article) => $article->isPublished())
->afterStateUpdated(
fn (Set $set, ?string $state) => $set('slug', Str::slug($state))
)
->helperText(fn (Article $article) => $article->isPublished()
? 'The slug cannot be changed after the article is published.'
: false
),
Textarea::make('excerpt')
->required()
->maxLength(400)
->columnSpanFull(),
MarkdownEditor::make('content')
->required()
->columnSpanFull(),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('title')
->searchable()
->sortable(),
TextColumn::make('author.name')
->label('Author')
->searchable()
->sortable(),
TextColumn::make('published_at')
->badge()
->dateTime('M j, Y H:i')
->color(fn ($state) => $state && $state->isPast() ? 'success' : 'warning')
->sortable(query: function (Builder $query, string $direction): Builder {
return $query->orderByRaw("published_at IS NULL {$direction}, published_at {$direction}");
}),
])
->filters([
//
])
->actions([
ActionGroup::make([
PreviewAction::make('preview'),
Actions\EditAction::make()->url(fn ($record) => static::getUrl('edit', ['record' => $record->id])),
UnpublishAction::make('unpublish'),
PublishAction::make('publish'),
]),
])
->bulkActions([
Actions\BulkActionGroup::make([
Actions\DeleteBulkAction::make(),
]),
])
->defaultSort('published_at', 'desc');
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListArticles::route('/'),
'create' => Pages\CreateArticle::route('/create'),
'edit' => Pages\EditArticle::route('/{record}/edit'),
];
}
}