-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathArticleResource.php
More file actions
115 lines (99 loc) · 3.44 KB
/
ArticleResource.php
File metadata and controls
115 lines (99 loc) · 3.44 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
<?php
namespace App\Filament\Resources;
use App\Filament\Resources\ArticleResource\Pages;
use App\Models\Article;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\MarkdownEditor;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Forms\Set;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
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 ?string $navigationIcon = 'heroicon-o-newspaper';
public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('title')
->required()
->maxLength(255),
TextInput::make('slug')
->required()
->maxLength(255)
->live(onBlur: true)
->unique(Article::class, 'slug', ignoreRecord: true)
->afterStateUpdated(fn (Set $set, ?string $state) => $set('slug', Str::slug($state))),
DateTimePicker::make('published_at')
->label('Published At')
->displayFormat('M j, Y H:i')
->seconds(false)
->dehydrated()
->reactive()
->default(now()),
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('excerpt')
->searchable()
->limit(50),
TextColumn::make('author.name')
->label('Author')
->searchable()
->sortable(),
TextColumn::make('published_at')
->dateTime('M j, Y H:i')
->sortable()
->badge()
->color(fn ($state) => $state && $state->isPast() ? 'success' : 'warning'),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make()
->url(fn ($record) => static::getUrl('edit', ['record' => $record->id])),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\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'),
];
}
}