Add Page resource to Filament admin panel: Implemented Page model, resource, and associated pages (create, edit, list). Defined page form schema and table configuration. Added migration for pages table.

This commit is contained in:
Ümit Tunç
2025-09-27 14:03:03 -03:00
parent c36d3a4738
commit d54601cb6b
8 changed files with 491 additions and 0 deletions
@@ -0,0 +1,58 @@
<?php
namespace App\Filament\Admin\Resources\Pages;
use App\Filament\Admin\Resources\Pages\Pages\CreatePage;
use App\Filament\Admin\Resources\Pages\Pages\EditPage;
use App\Filament\Admin\Resources\Pages\Pages\ListPages;
use App\Filament\Admin\Resources\Pages\Schemas\PageForm;
use App\Filament\Admin\Resources\Pages\Tables\PagesTable;
use App\Models\Page;
use BackedEnum;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
class PageResource extends Resource
{
protected static ?string $model = Page::class;
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedRectangleStack;
public static function form(Schema $schema): Schema
{
return PageForm::configure($schema);
}
public static function table(Table $table): Table
{
return PagesTable::configure($table);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => ListPages::route('/'),
'create' => CreatePage::route('/create'),
'edit' => EditPage::route('/{record}/edit'),
];
}
public static function getRecordRouteBindingEloquentQuery(): Builder
{
return parent::getRecordRouteBindingEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
}
@@ -0,0 +1,11 @@
<?php
namespace App\Filament\Admin\Resources\Pages\Pages;
use App\Filament\Admin\Resources\Pages\PageResource;
use Filament\Resources\Pages\CreateRecord;
class CreatePage extends CreateRecord
{
protected static string $resource = PageResource::class;
}
@@ -0,0 +1,23 @@
<?php
namespace App\Filament\Admin\Resources\Pages\Pages;
use App\Filament\Admin\Resources\Pages\PageResource;
use Filament\Actions\DeleteAction;
use Filament\Actions\ForceDeleteAction;
use Filament\Actions\RestoreAction;
use Filament\Resources\Pages\EditRecord;
class EditPage extends EditRecord
{
protected static string $resource = PageResource::class;
protected function getHeaderActions(): array
{
return [
DeleteAction::make(),
ForceDeleteAction::make(),
RestoreAction::make(),
];
}
}
@@ -0,0 +1,19 @@
<?php
namespace App\Filament\Admin\Resources\Pages\Pages;
use App\Filament\Admin\Resources\Pages\PageResource;
use Filament\Actions\CreateAction;
use Filament\Resources\Pages\ListRecords;
class ListPages extends ListRecords
{
protected static string $resource = PageResource::class;
protected function getHeaderActions(): array
{
return [
CreateAction::make(),
];
}
}
@@ -0,0 +1,133 @@
<?php
namespace App\Filament\Admin\Resources\Pages\Schemas;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Schema;
class PageForm
{
public static function configure(Schema $schema): Schema
{
return $schema
->components([
TextInput::make('title')
->label('Başlık')
->required()
->maxLength(255)
->live(onBlur: true)
->afterStateUpdated(function (string $operation, $state, callable $set) {
if ($operation !== 'create') {
return;
}
$set('slug', \Str::slug($state));
}),
TextInput::make('slug')
->label('URL Slug')
->required()
->maxLength(255)
->unique(ignoreRecord: true)
->rules(['alpha_dash'])
->helperText('URL\'de görünecek kısım. Örnek: hakkimizda'),
Textarea::make('excerpt')
->label('Özet')
->rows(3)
->maxLength(500)
->helperText('Sayfa özeti (maksimum 500 karakter)'),
RichEditor::make('content')
->label('İçerik')
->required()
->fileAttachmentsDisk('public')
->fileAttachmentsDirectory('pages')
->fileAttachmentsVisibility('public'),
Select::make('status')
->label('Durum')
->options([
'draft' => 'Taslak',
'published' => 'Yayında',
'archived' => 'Arşivlendi',
])
->default('draft')
->required(),
DateTimePicker::make('published_at')
->label('Yayın Tarihi')
->displayFormat('d.m.Y H:i')
->helperText('Boş bırakılırsa şu anki tarih kullanılır'),
Select::make('author_id')
->label('Yazar')
->relationship('author', 'name')
->default(auth()->id())
->required(),
Select::make('parent_id')
->label('Üst Sayfa')
->relationship('parent', 'title')
->searchable()
->preload()
->helperText('Bu sayfayı başka bir sayfanın alt sayfası yapmak için seçin'),
TextInput::make('sort_order')
->label('Sıra')
->numeric()
->default(0)
->helperText('Menüde görünme sırası'),
Select::make('template')
->label('Şablon')
->options([
'default' => 'Varsayılan',
'landing' => 'Landing Page',
'blog' => 'Blog',
'contact' => 'İletişim',
])
->default('default'),
Checkbox::make('is_homepage')
->label('Ana Sayfa')
->helperText('Bu sayfayı ana sayfa olarak ayarla'),
Checkbox::make('show_in_menu')
->label('Menüde Göster')
->default(true)
->helperText('Bu sayfa menüde görünsün mü?'),
TextInput::make('meta_title')
->label('Meta Başlık')
->maxLength(60)
->helperText('Arama motorları için başlık (maksimum 60 karakter)'),
Textarea::make('meta_description')
->label('Meta Açıklama')
->rows(3)
->maxLength(160)
->helperText('Arama motorları için açıklama (maksimum 160 karakter)'),
FileUpload::make('featured_image')
->label('Öne Çıkan Görsel')
->image()
->disk('public')
->directory('pages/featured')
->visibility('public')
->imageEditor()
->imageEditorAspectRatios([
'16:9',
'4:3',
'1:1',
])
->helperText('Sayfa için öne çıkan görsel seçin'),
]);
}
}
@@ -0,0 +1,134 @@
<?php
namespace App\Filament\Admin\Resources\Pages\Tables;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ForceDeleteBulkAction;
use Filament\Actions\RestoreBulkAction;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;
class PagesTable
{
public static function configure(Table $table): Table
{
return $table
->columns([
ImageColumn::make('featured_image')
->label('Görsel')
->circular()
->size(40)
->defaultImageUrl('/images/placeholder-page.png'),
TextColumn::make('title')
->label('Başlık')
->searchable()
->sortable()
->weight('bold')
->description(fn ($record) => $record->excerpt ? \Str::limit($record->excerpt, 50) : null),
TextColumn::make('slug')
->label('URL')
->searchable()
->sortable()
->copyable()
->copyMessage('URL kopyalandı')
->url(fn ($record) => $record->url, shouldOpenInNewTab: true)
->color('primary'),
TextColumn::make('status')
->label('Durum')
->badge()
->color(fn (string $state): string => match ($state) {
'published' => 'success',
'draft' => 'warning',
'archived' => 'gray',
})
->formatStateUsing(fn (string $state): string => match ($state) {
'published' => 'Yayında',
'draft' => 'Taslak',
'archived' => 'Arşivlendi',
}),
TextColumn::make('author.name')
->label('Yazar')
->searchable()
->sortable()
->toggleable(),
TextColumn::make('published_at')
->label('Yayın Tarihi')
->dateTime('d.m.Y H:i')
->sortable()
->toggleable(),
TextColumn::make('parent.title')
->label('Üst Sayfa')
->searchable()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
TextColumn::make('template')
->label('Şablon')
->badge()
->color('info')
->toggleable(isToggledHiddenByDefault: true),
ToggleColumn::make('is_homepage')
->label('Ana Sayfa')
->toggleable(isToggledHiddenByDefault: true),
ToggleColumn::make('show_in_menu')
->label('Menüde Göster')
->toggleable(isToggledHiddenByDefault: true),
TextColumn::make('sort_order')
->label('Sıra')
->sortable()
->toggleable(isToggledHiddenByDefault: true),
TextColumn::make('created_at')
->label('Oluşturulma')
->dateTime('d.m.Y H:i')
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
SelectFilter::make('status')
->label('Durum')
->options([
'published' => 'Yayında',
'draft' => 'Taslak',
'archived' => 'Arşivlendi',
]),
SelectFilter::make('template')
->label('Şablon')
->options([
'default' => 'Varsayılan',
'landing' => 'Landing Page',
'blog' => 'Blog',
'contact' => 'İletişim',
]),
TrashedFilter::make(),
])
->recordActions([
EditAction::make(),
])
->toolbarActions([
BulkActionGroup::make([
DeleteBulkAction::make(),
ForceDeleteBulkAction::make(),
RestoreBulkAction::make(),
]),
]);
}
}
+66
View File
@@ -0,0 +1,66 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Page extends Model
{
use HasFactory, SoftDeletes;
protected $fillable = [
'title',
'slug',
'content',
'excerpt',
'meta_title',
'meta_description',
'status',
'featured_image',
'published_at',
'author_id',
'parent_id',
'sort_order',
'template',
'is_homepage',
'show_in_menu',
];
protected $casts = [
'published_at' => 'datetime',
'is_homepage' => 'boolean',
'show_in_menu' => 'boolean',
'sort_order' => 'integer',
];
public function author()
{
return $this->belongsTo(User::class, 'author_id');
}
public function parent()
{
return $this->belongsTo(Page::class, 'parent_id');
}
public function children()
{
return $this->hasMany(Page::class, 'parent_id');
}
public function getRouteKeyName()
{
return 'slug';
}
public function getUrlAttribute()
{
if ($this->is_homepage) {
return '/';
}
return '/' . $this->slug;
}
}
@@ -0,0 +1,47 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('pages', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('slug')->unique();
$table->longText('content')->nullable();
$table->text('excerpt')->nullable();
$table->string('meta_title')->nullable();
$table->text('meta_description')->nullable();
$table->enum('status', ['draft', 'published', 'archived'])->default('draft');
$table->string('featured_image')->nullable();
$table->timestamp('published_at')->nullable();
$table->foreignId('author_id')->constrained('users')->onDelete('cascade');
$table->foreignId('parent_id')->nullable()->constrained('pages')->onDelete('cascade');
$table->integer('sort_order')->default(0);
$table->string('template')->default('default');
$table->boolean('is_homepage')->default(false);
$table->boolean('show_in_menu')->default(true);
$table->timestamps();
$table->softDeletes();
$table->index(['status', 'published_at']);
$table->index(['parent_id', 'sort_order']);
$table->index('slug');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('pages');
}
};