M3: Filament 后台资源(文章/分类/评论/媒体/用户/友链)+ 博客设置页 + 仪表盘统计
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Comments;
|
||||
|
||||
use App\Filament\Resources\Comments\Pages\CreateComment;
|
||||
use App\Filament\Resources\Comments\Pages\EditComment;
|
||||
use App\Filament\Resources\Comments\Pages\ListComments;
|
||||
use App\Filament\Resources\Comments\Schemas\CommentForm;
|
||||
use App\Filament\Resources\Comments\Tables\CommentsTable;
|
||||
use App\Models\Comment;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class CommentResource extends Resource
|
||||
{
|
||||
protected static \UnitEnum|string|null $navigationGroup = '内容';
|
||||
|
||||
protected static ?string $model = Comment::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedRectangleStack;
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return CommentForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return CommentsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListComments::route('/'),
|
||||
'create' => CreateComment::route('/create'),
|
||||
'edit' => EditComment::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Comments\Pages;
|
||||
|
||||
use App\Filament\Resources\Comments\CommentResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateComment extends CreateRecord
|
||||
{
|
||||
protected static string $resource = CommentResource::class;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Comments\Pages;
|
||||
|
||||
use App\Filament\Resources\Comments\CommentResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditComment extends EditRecord
|
||||
{
|
||||
protected static string $resource = CommentResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Comments\Pages;
|
||||
|
||||
use App\Filament\Resources\Comments\CommentResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListComments extends ListRecords
|
||||
{
|
||||
protected static string $resource = CommentResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Comments\Schemas;
|
||||
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class CommentForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
TextInput::make('author_name')
|
||||
->label('作者')
|
||||
->required(),
|
||||
TextInput::make('author_email')
|
||||
->label('邮箱'),
|
||||
TextInput::make('author_url')
|
||||
->label('网站')
|
||||
->url(),
|
||||
Textarea::make('content')
|
||||
->label('内容')
|
||||
->required()
|
||||
->rows(5),
|
||||
Select::make('status')
|
||||
->label('状态')
|
||||
->options([
|
||||
'published' => '已发布',
|
||||
'pending' => '待审核',
|
||||
'spam' => '垃圾',
|
||||
'rejected' => '已拒绝',
|
||||
])
|
||||
->required(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Comments\Tables;
|
||||
|
||||
use App\Models\Comment;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class CommentsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('author_name')
|
||||
->label('作者')
|
||||
->searchable()
|
||||
->weight('bold'),
|
||||
TextColumn::make('content')
|
||||
->label('内容')
|
||||
->limit(60)
|
||||
->searchable(),
|
||||
TextColumn::make('post.title')
|
||||
->label('文章')
|
||||
->limit(30)
|
||||
->url(fn ($record) => $record->post ? route('posts.show', $record->post->slug ?: $record->post->id) : null)
|
||||
->openUrlInNewTab(),
|
||||
TextColumn::make('status')
|
||||
->label('状态')
|
||||
->badge()
|
||||
->formatStateUsing(fn ($state) => match ($state) {
|
||||
'published' => '已发布',
|
||||
'pending' => '待审核',
|
||||
'spam' => '垃圾',
|
||||
'rejected' => '已拒绝',
|
||||
default => $state,
|
||||
})
|
||||
->color(fn ($state) => match ($state) {
|
||||
'published' => 'success',
|
||||
'pending' => 'warning',
|
||||
'spam' => 'danger',
|
||||
default => 'gray',
|
||||
}),
|
||||
TextColumn::make('ip')
|
||||
->label('IP'),
|
||||
TextColumn::make('created_at')
|
||||
->label('时间')
|
||||
->dateTime('Y-m-d H:i')
|
||||
->sortable(),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('status')
|
||||
->label('状态')
|
||||
->options([
|
||||
'published' => '已发布',
|
||||
'pending' => '待审核',
|
||||
'spam' => '垃圾',
|
||||
'rejected' => '已拒绝',
|
||||
]),
|
||||
])
|
||||
->recordActions([
|
||||
Action::make('approve')
|
||||
->label('通过')
|
||||
->icon('heroicon-o-check')
|
||||
->color('success')
|
||||
->visible(fn (Comment $record) => $record->status !== 'published')
|
||||
->action(function (Comment $record): void {
|
||||
$record->update(['status' => 'published']);
|
||||
$record->post?->increment('comment_count');
|
||||
}),
|
||||
Action::make('spam')
|
||||
->label('垃圾')
|
||||
->icon('heroicon-o-x-mark')
|
||||
->color('danger')
|
||||
->visible(fn (Comment $record) => $record->status !== 'spam')
|
||||
->action(fn (Comment $record) => $record->update(['status' => 'spam'])),
|
||||
EditAction::make(),
|
||||
])
|
||||
->toolbarActions([
|
||||
BulkActionGroup::make([
|
||||
DeleteBulkAction::make(),
|
||||
]),
|
||||
])
|
||||
->defaultSort('created_at', 'desc');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user