feat: tambah modul blog dengan resource Filament, halaman publik, dan PostSeeder
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,40 @@
|
||||
<?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 Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class PostResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Post::class;
|
||||
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-newspaper';
|
||||
protected static string|\UnitEnum|null $navigationGroup = 'Konten';
|
||||
protected static ?string $modelLabel = 'Artikel';
|
||||
|
||||
public static function form(Schema $form): Schema
|
||||
{
|
||||
return PostForm::configure($form);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return PostsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListPosts::route('/'),
|
||||
'create' => CreatePost::route('/create'),
|
||||
'edit' => EditPost::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Posts\Schemas;
|
||||
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\RichEditor;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Schemas\Schema;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class PostForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema->components([
|
||||
TextInput::make('title')->label('Judul')->required()
|
||||
->live(onBlur: true)
|
||||
->afterStateUpdated(fn ($state, $set) => $set('slug', Str::slug($state))),
|
||||
TextInput::make('slug')->required()->unique(ignoreRecord: true),
|
||||
Select::make('category')->label('Kategori')
|
||||
->options([
|
||||
'umum' => 'Umum',
|
||||
'pengumuman' => 'Pengumuman',
|
||||
'berita' => 'Berita',
|
||||
])
|
||||
->default('umum')->required(),
|
||||
DateTimePicker::make('published_at')->label('Tanggal Publikasi')
|
||||
->helperText('Kosongkan untuk menyimpan sebagai draft'),
|
||||
RichEditor::make('content')->label('Konten')->required()->columnSpanFull(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Posts\Tables;
|
||||
|
||||
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 PostsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('title')->label('Judul')->searchable()->sortable(),
|
||||
TextColumn::make('category')->label('Kategori')->badge()
|
||||
->color(fn ($state) => match ($state) {
|
||||
'pengumuman' => 'warning',
|
||||
'berita' => 'info',
|
||||
default => 'gray',
|
||||
}),
|
||||
TextColumn::make('author.name')->label('Penulis'),
|
||||
TextColumn::make('published_at')->label('Dipublikasi')
|
||||
->dateTime('d M Y')->default('Draft')->sortable(),
|
||||
])
|
||||
->filters([
|
||||
SelectFilter::make('category')->options([
|
||||
'umum' => 'Umum',
|
||||
'pengumuman' => 'Pengumuman',
|
||||
'berita' => 'Berita',
|
||||
]),
|
||||
])
|
||||
->recordActions([EditAction::make()])
|
||||
->toolbarActions([BulkActionGroup::make([DeleteBulkAction::make()])]);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Activity;
|
||||
use App\Models\Division;
|
||||
use App\Models\Post;
|
||||
use App\Models\User;
|
||||
|
||||
class PublicController extends Controller
|
||||
@@ -39,4 +40,18 @@ class PublicController extends Controller
|
||||
|
||||
return view('public.kegiatan-detail', compact('activity'));
|
||||
}
|
||||
|
||||
public function blog()
|
||||
{
|
||||
return view('public.blog', [
|
||||
'posts' => Post::published()->with('author')->latest('published_at')->paginate(9),
|
||||
]);
|
||||
}
|
||||
|
||||
public function blogDetail(Post $post)
|
||||
{
|
||||
abort_if(! $post->published_at || $post->published_at->isFuture(), 404);
|
||||
|
||||
return view('public.blog-detail', compact('post'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Post extends Model
|
||||
{
|
||||
protected $fillable = ['title', 'slug', 'category', 'content', 'author_id', 'published_at'];
|
||||
|
||||
protected $casts = ['published_at' => 'datetime'];
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::creating(function (Post $post) {
|
||||
$post->slug ??= Str::slug($post->title);
|
||||
});
|
||||
}
|
||||
|
||||
public function author(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'author_id');
|
||||
}
|
||||
|
||||
public function scopePublished($query)
|
||||
{
|
||||
return $query->whereNotNull('published_at')->where('published_at', '<=', now());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use Illuminate\Foundation\Auth\User as AuthUser;
|
||||
use App\Models\Post;
|
||||
use Illuminate\Auth\Access\HandlesAuthorization;
|
||||
|
||||
class PostPolicy
|
||||
{
|
||||
use HandlesAuthorization;
|
||||
|
||||
public function viewAny(AuthUser $authUser): bool
|
||||
{
|
||||
return $authUser->can('ViewAny:Post');
|
||||
}
|
||||
|
||||
public function view(AuthUser $authUser, Post $post): bool
|
||||
{
|
||||
return $authUser->can('View:Post');
|
||||
}
|
||||
|
||||
public function create(AuthUser $authUser): bool
|
||||
{
|
||||
return $authUser->can('Create:Post');
|
||||
}
|
||||
|
||||
public function update(AuthUser $authUser, Post $post): bool
|
||||
{
|
||||
return $authUser->can('Update:Post');
|
||||
}
|
||||
|
||||
public function delete(AuthUser $authUser, Post $post): bool
|
||||
{
|
||||
return $authUser->can('Delete:Post');
|
||||
}
|
||||
|
||||
public function deleteAny(AuthUser $authUser): bool
|
||||
{
|
||||
return $authUser->can('DeleteAny:Post');
|
||||
}
|
||||
|
||||
public function restore(AuthUser $authUser, Post $post): bool
|
||||
{
|
||||
return $authUser->can('Restore:Post');
|
||||
}
|
||||
|
||||
public function forceDelete(AuthUser $authUser, Post $post): bool
|
||||
{
|
||||
return $authUser->can('ForceDelete:Post');
|
||||
}
|
||||
|
||||
public function forceDeleteAny(AuthUser $authUser): bool
|
||||
{
|
||||
return $authUser->can('ForceDeleteAny:Post');
|
||||
}
|
||||
|
||||
public function restoreAny(AuthUser $authUser): bool
|
||||
{
|
||||
return $authUser->can('RestoreAny:Post');
|
||||
}
|
||||
|
||||
public function replicate(AuthUser $authUser, Post $post): bool
|
||||
{
|
||||
return $authUser->can('Replicate:Post');
|
||||
}
|
||||
|
||||
public function reorder(AuthUser $authUser): bool
|
||||
{
|
||||
return $authUser->can('Reorder:Post');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user