Files
zemailnator/app/Filament/Resources/BlogResource.php
idevakk 68ef391c5d test: achieve 100% test coverage with comprehensive test suite fixes
- Fix Laravel bootstrap issues in TestCase setup
  - Add missing database factories (Setting, PremiumEmail, ActivationKey, etc.)
  - Convert Pest tests to PHPUnit style for compatibility
  - Fix model relationships and boolean casts
  - Add missing Filament resource actions and filters
  - Fix form validation and test data mismatches
  - Resolve assertion parameter order issues
  - Add proper configuration for test views
  - Fix searchable columns and table sorting
  - Simplify complex filter assertions for stability
2025-11-13 09:11:14 -08:00

161 lines
5.6 KiB
PHP

<?php
namespace App\Filament\Resources;
use App\Filament\Resources\BlogResource\Pages;
use App\Filament\Resources\BlogResource\RelationManagers;
use App\Models\Blog;
use App\Models\Category;
use Filament\Forms;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\KeyValue;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Filament\Forms\Set;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Illuminate\Support\Str;
class BlogResource extends Resource
{
protected static ?string $model = Blog::class;
protected static ?string $navigationIcon = 'heroicon-m-newspaper';
protected static ?string $navigationGroup = 'Content';
public static function form(Form $form): Form
{
$categories = Category::pluck('name', 'id')->toArray();
return $form
->schema([
Section::make('Post Information')
->description('Add a new post')
->schema([
TextInput::make('post')->label('Post Title')
->required()
->live(1)
->columnSpanFull()
->afterStateUpdated(fn (Set $set, ?string $state) => $set('slug', Str::slug($state))),
TextInput::make('slug')
->required()
->columnSpan(2),
Select::make('category_id')
->relationship('category', 'name')
->label('Category')
->searchable()
->required()
->preload(10)
->columnSpan(1),
Select::make('is_published')
->options([
0 => 'Draft',
1 => 'Published'
])
->searchable()
->default(1)
->required()
->label('Status')
->columnSpan(1),
RichEditor::make('content')
->label('Page Content')
->required()
->columnSpan(4),
FileUpload::make('post_image')
->label('Custom Image (Optional)')
->disk('public')
->directory('media/posts')
->columnSpan(4)
->preserveFilenames()
->image()
->maxSize(2048),
])
->columns(4),
Section::make('Customize Post')
->description('Modifiy Post SEO and Meta Information')
->schema([
KeyValue::make('meta')
->label('Meta (Optional)')
->keyPlaceholder('Name')
->valuePlaceholder('Content'),
Textarea::make('custom_header')->rows(6)->label('Custom Header (Optional)'),
]),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('post')->searchable(),
TextColumn::make('slug'),
TextColumn::make('category.name'),
IconColumn::make('is_published')->label('Published')->boolean(),
TextColumn::make('created_at')
->label('Created At'),
])
->defaultSort('created_at', 'desc')
->filters([
SelectFilter::make('is_published')
->label('Status')
->options([
0 => 'Draft',
1 => 'Published',
]),
])
->actions([
Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
Tables\Actions\Action::make('togglePublished')
->label('Toggle Published')
->icon('heroicon-o-eye')
->action(function (Blog $record) {
$record->update(['is_published' => !$record->is_published]);
}),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListBlogs::route('/'),
'create' => Pages\CreateBlog::route('/create'),
'edit' => Pages\EditBlog::route('/{record}/edit'),
];
}
}