Skip to content

Commit 9b62780

Browse files
committed
add filament resource
1 parent 9136db1 commit 9b62780

File tree

5 files changed

+188
-0
lines changed

5 files changed

+188
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace App\Filament\Resources;
4+
5+
use App\Filament\Resources\ArticleResource\Pages;
6+
use App\Models\Article;
7+
use Filament\Forms\Components\DateTimePicker;
8+
use Filament\Forms\Components\MarkdownEditor;
9+
use Filament\Forms\Components\Textarea;
10+
use Filament\Forms\Components\TextInput;
11+
use Filament\Forms\Form;
12+
use Filament\Forms\Set;
13+
use Filament\Resources\Resource;
14+
use Filament\Tables;
15+
use Filament\Tables\Columns\TextColumn;
16+
use Filament\Tables\Table;
17+
use Illuminate\Support\Str;
18+
19+
class ArticleResource extends Resource
20+
{
21+
protected static ?string $model = Article::class;
22+
23+
protected static ?string $recordRouteKeyName = 'id';
24+
25+
protected static ?string $recordTitleAttribute = 'title';
26+
27+
protected static ?string $navigationIcon = 'heroicon-o-newspaper';
28+
29+
public static function form(Form $form): Form
30+
{
31+
return $form
32+
->schema([
33+
TextInput::make('title')
34+
->required()
35+
->maxLength(255),
36+
37+
TextInput::make('slug')
38+
->required()
39+
->maxLength(255)
40+
->live(onBlur: true)
41+
->unique(Article::class, 'slug', ignoreRecord: true)
42+
->afterStateUpdated(fn (Set $set, ?string $state) => $set('slug', Str::slug($state))),
43+
44+
DateTimePicker::make('published_at')
45+
->label('Published At')
46+
->displayFormat('M j, Y H:i')
47+
->seconds(false)
48+
->dehydrated()
49+
->reactive()
50+
->default(now()),
51+
52+
Textarea::make('excerpt')
53+
->required()
54+
->maxLength(400)
55+
->columnSpanFull(),
56+
57+
MarkdownEditor::make('content')
58+
->required()
59+
->columnSpanFull(),
60+
]);
61+
}
62+
63+
public static function table(Table $table): Table
64+
{
65+
return $table
66+
->columns([
67+
TextColumn::make('title')
68+
->searchable()
69+
->sortable(),
70+
71+
TextColumn::make('excerpt')
72+
->searchable()
73+
->limit(50),
74+
75+
TextColumn::make('author.name')
76+
->label('Author')
77+
->searchable()
78+
->sortable(),
79+
TextColumn::make('published_at')
80+
->dateTime('M j, Y H:i')
81+
->sortable()
82+
->badge()
83+
->color(fn ($state) => $state && $state->isPast() ? 'success' : 'warning'),
84+
])
85+
->filters([
86+
//
87+
])
88+
->actions([
89+
Tables\Actions\EditAction::make()
90+
->url(fn ($record) => static::getUrl('edit', ['record' => $record->id])),
91+
])
92+
->bulkActions([
93+
Tables\Actions\BulkActionGroup::make([
94+
Tables\Actions\DeleteBulkAction::make(),
95+
]),
96+
])
97+
->defaultSort('published_at', 'desc');
98+
}
99+
100+
public static function getRelations(): array
101+
{
102+
return [
103+
//
104+
];
105+
}
106+
107+
public static function getPages(): array
108+
{
109+
return [
110+
'index' => Pages\ListArticles::route('/'),
111+
'create' => Pages\CreateArticle::route('/create'),
112+
'edit' => Pages\EditArticle::route('/{record}/edit'),
113+
];
114+
}
115+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Filament\Resources\ArticleResource\Pages;
4+
5+
use App\Filament\Resources\ArticleResource;
6+
use Filament\Resources\Pages\CreateRecord;
7+
8+
class CreateArticle extends CreateRecord
9+
{
10+
protected static string $resource = ArticleResource::class;
11+
12+
protected function getRedirectUrl(): string
13+
{
14+
return static::getResource()::getUrl('edit', ['record' => $this->record->id]);
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Filament\Resources\ArticleResource\Pages;
4+
5+
use App\Filament\Resources\ArticleResource;
6+
use Filament\Actions;
7+
use Filament\Resources\Pages\EditRecord;
8+
9+
class EditArticle extends EditRecord
10+
{
11+
protected static string $resource = ArticleResource::class;
12+
13+
protected function getHeaderActions(): array
14+
{
15+
return [
16+
Actions\DeleteAction::make(),
17+
];
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Filament\Resources\ArticleResource\Pages;
4+
5+
use App\Filament\Resources\ArticleResource;
6+
use Filament\Actions;
7+
use Filament\Resources\Pages\ListRecords;
8+
9+
class ListArticles extends ListRecords
10+
{
11+
protected static string $resource = ArticleResource::class;
12+
13+
protected function getHeaderActions(): array
14+
{
15+
return [
16+
Actions\CreateAction::make(),
17+
];
18+
}
19+
}

app/Models/Article.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ class Article extends Model
1111
{
1212
use HasFactory;
1313

14+
protected $fillable = [
15+
'slug',
16+
'title',
17+
'excerpt',
18+
'content',
19+
'published_at',
20+
];
21+
1422
protected $casts = [
1523
'published_at' => 'datetime',
1624
];
@@ -32,4 +40,15 @@ public function author(): BelongsTo
3240
{
3341
return $this->belongsTo(User::class, 'author_id');
3442
}
43+
44+
protected static function boot()
45+
{
46+
parent::boot();
47+
48+
static::creating(function ($article) {
49+
if (auth()->check() && ! $article->author_id) {
50+
$article->author_id = auth()->id();
51+
}
52+
});
53+
}
3554
}

0 commit comments

Comments
 (0)