M3: Filament 后台资源(文章/分类/评论/媒体/用户/友链)+ 博客设置页 + 仪表盘统计
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Posts\Pages;
|
||||
|
||||
use App\Filament\Resources\Posts\PostResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreatePost extends CreateRecord
|
||||
{
|
||||
protected static string $resource = PostResource::class;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Posts\Pages;
|
||||
|
||||
use App\Filament\Resources\Posts\PostResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditPost extends EditRecord
|
||||
{
|
||||
protected static string $resource = PostResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Posts\Pages;
|
||||
|
||||
use App\Filament\Resources\Posts\PostResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListPosts extends ListRecords
|
||||
{
|
||||
protected static string $resource = PostResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Posts;
|
||||
|
||||
use App\Filament\Resources\Posts\Pages\CreatePost;
|
||||
use App\Filament\Resources\Posts\Pages\EditPost;
|
||||
use App\Filament\Resources\Posts\Pages\ListPosts;
|
||||
use App\Filament\Resources\Posts\Schemas\PostForm;
|
||||
use App\Filament\Resources\Posts\Tables\PostsTable;
|
||||
use App\Models\Post;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class PostResource extends Resource
|
||||
{
|
||||
protected static \UnitEnum|string|null $navigationGroup = '内容';
|
||||
|
||||
protected static ?string $model = Post::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedRectangleStack;
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return PostForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return PostsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListPosts::route('/'),
|
||||
'create' => CreatePost::route('/create'),
|
||||
'edit' => EditPost::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Posts\Schemas;
|
||||
|
||||
use App\Support\MediaDisk;
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\MarkdownEditor;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
|
||||
|
||||
class PostForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
\Filament\Schemas\Components\Section::make('内容')
|
||||
->columns(2)
|
||||
->schema([
|
||||
TextInput::make('title')
|
||||
->label('标题')
|
||||
->required()
|
||||
->columnSpanFull(),
|
||||
TextInput::make('slug')
|
||||
->label('Slug(留空自动生成)'),
|
||||
Select::make('category_id')
|
||||
->label('分类')
|
||||
->relationship('category', 'name'),
|
||||
Select::make('content_format')
|
||||
->label('内容格式')
|
||||
->options([
|
||||
'markdown' => 'Markdown',
|
||||
'html' => 'HTML',
|
||||
])
|
||||
->default('markdown'),
|
||||
Select::make('status')
|
||||
->label('状态')
|
||||
->options([
|
||||
'published' => '已发布',
|
||||
'draft' => '草稿',
|
||||
'private' => '私密',
|
||||
])
|
||||
->default('published'),
|
||||
MarkdownEditor::make('content')
|
||||
->label('正文')
|
||||
->required()
|
||||
->columnSpanFull(),
|
||||
]),
|
||||
\Filament\Schemas\Components\Section::make('封面与摘要')
|
||||
->columns(2)
|
||||
->schema([
|
||||
SpatieMediaLibraryFileUpload::make('cover')
|
||||
->label('封面图')
|
||||
->collection('cover')
|
||||
->image()
|
||||
->imageEditor()
|
||||
->disk(MediaDisk::name())
|
||||
->columnSpanFull(),
|
||||
Textarea::make('excerpt')
|
||||
->label('摘要(留空自动截取)')
|
||||
->rows(3),
|
||||
TextInput::make('keywords')
|
||||
->label('关键词(逗号分隔)'),
|
||||
]),
|
||||
\Filament\Schemas\Components\Section::make('发布设置')
|
||||
->columns(3)
|
||||
->schema([
|
||||
DateTimePicker::make('published_at')
|
||||
->label('发布时间')
|
||||
->default(now()),
|
||||
Toggle::make('is_sticky')
|
||||
->label('置顶'),
|
||||
Toggle::make('close_comment')
|
||||
->label('关闭评论'),
|
||||
TextInput::make('read_password')
|
||||
->label('阅读密码(留空公开)'),
|
||||
TextInput::make('views')
|
||||
->label('浏览量')
|
||||
->numeric()
|
||||
->disabled(),
|
||||
TextInput::make('comment_count')
|
||||
->label('评论数')
|
||||
->numeric()
|
||||
->disabled(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Posts\Tables;
|
||||
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Filters\TernaryFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class PostsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('title')
|
||||
->label('标题')
|
||||
->searchable()
|
||||
->limit(40)
|
||||
->description(fn ($record) => $record->category?->name),
|
||||
TextColumn::make('content_format')
|
||||
->label('格式')
|
||||
->badge()
|
||||
->formatStateUsing(fn ($state) => $state === 'markdown' ? 'MD' : 'HTML')
|
||||
->color(fn ($state) => $state === 'markdown' ? 'success' : 'gray'),
|
||||
TextColumn::make('status')
|
||||
->label('状态')
|
||||
->badge()
|
||||
->formatStateUsing(fn ($state) => match ($state) {
|
||||
'published' => '已发布',
|
||||
'draft' => '草稿',
|
||||
'private' => '私密',
|
||||
default => $state,
|
||||
})
|
||||
->color(fn ($state) => match ($state) {
|
||||
'published' => 'success',
|
||||
'draft' => 'warning',
|
||||
default => 'gray',
|
||||
}),
|
||||
IconColumn::make('is_sticky')
|
||||
->label('置顶')
|
||||
->boolean(),
|
||||
TextColumn::make('views')
|
||||
->label('阅读')
|
||||
->numeric()
|
||||
->sortable(),
|
||||
TextColumn::make('comment_count')
|
||||
->label('评论')
|
||||
->numeric()
|
||||
->sortable(),
|
||||
TextColumn::make('published_at')
|
||||
->label('发布时间')
|
||||
->dateTime('Y-m-d H:i')
|
||||
->sortable(),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('status')
|
||||
->label('状态')
|
||||
->options([
|
||||
'published' => '已发布',
|
||||
'draft' => '草稿',
|
||||
'private' => '私密',
|
||||
]),
|
||||
SelectFilter::make('content_format')
|
||||
->label('格式')
|
||||
->options([
|
||||
'markdown' => 'Markdown',
|
||||
'html' => 'HTML',
|
||||
]),
|
||||
TernaryFilter::make('is_sticky')
|
||||
->label('置顶'),
|
||||
])
|
||||
->recordActions([
|
||||
ViewAction::make(),
|
||||
EditAction::make(),
|
||||
])
|
||||
->toolbarActions([
|
||||
BulkActionGroup::make([
|
||||
DeleteBulkAction::make(),
|
||||
]),
|
||||
])
|
||||
->defaultSort('published_at', 'desc');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user