feat: tambah modul blog dengan resource Filament, halaman publik, dan PostSeeder

This commit is contained in:
2026-04-03 05:18:34 +07:00
parent 2646adf160
commit 3339a45243
16 changed files with 413 additions and 0 deletions
@@ -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()])]);
}
}
+15
View File
@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use App\Models\Activity; use App\Models\Activity;
use App\Models\Division; use App\Models\Division;
use App\Models\Post;
use App\Models\User; use App\Models\User;
class PublicController extends Controller class PublicController extends Controller
@@ -39,4 +40,18 @@ class PublicController extends Controller
return view('public.kegiatan-detail', compact('activity')); 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'));
}
} }
+31
View File
@@ -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());
}
}
+75
View File
@@ -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');
}
}
@@ -0,0 +1,27 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('slug')->unique();
$table->string('category')->default('umum'); // umum, pengumuman, berita
$table->longText('content');
$table->foreignId('author_id')->constrained('users');
$table->timestamp('published_at')->nullable();
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('posts');
}
};
+1
View File
@@ -16,6 +16,7 @@ class DatabaseSeeder extends Seeder
ActivitySeeder::class, ActivitySeeder::class,
CashSeeder::class, CashSeeder::class,
VoteSeeder::class, VoteSeeder::class,
PostSeeder::class,
]); ]);
} }
} }
+43
View File
@@ -0,0 +1,43 @@
<?php
namespace Database\Seeders;
use App\Models\Post;
use App\Models\User;
use Illuminate\Database\Seeder;
class PostSeeder extends Seeder
{
public function run(): void
{
$author = User::role('ketua')->first() ?? User::first();
$posts = [
[
'title' => 'Selamat Datang di Website Persegi',
'category' => 'pengumuman',
'content' => '<p>Kami dengan bangga mempersembahkan website resmi organisasi Persegi. Melalui website ini, masyarakat dapat mengikuti perkembangan kegiatan dan informasi terbaru dari organisasi kami.</p>',
'published_at' => now()->subDays(10),
],
[
'title' => 'Rekrutmen Anggota Baru 2026',
'category' => 'pengumuman',
'content' => '<p>Persegi membuka pendaftaran anggota baru untuk periode 2026. Bagi pemuda Desa Karangdadap yang ingin bergabung, silakan hubungi pengurus melalui kontak yang tersedia.</p><p>Pendaftaran dibuka hingga akhir bulan April 2026.</p>',
'published_at' => now()->subDays(5),
],
[
'title' => 'Laporan Kegiatan Kerja Bakti Desa',
'category' => 'berita',
'content' => '<p>Kegiatan kerja bakti yang dilaksanakan pada bulan lalu berjalan dengan lancar. Sebanyak 30 anggota turut berpartisipasi dalam membersihkan lingkungan desa.</p><p>Terima kasih kepada seluruh anggota yang telah berkontribusi.</p>',
'published_at' => now()->subDays(2),
],
];
foreach ($posts as $data) {
Post::firstOrCreate(
['slug' => \Illuminate\Support\Str::slug($data['title'])],
array_merge($data, ['author_id' => $author->id])
);
}
}
}
@@ -0,0 +1,26 @@
@extends('public.layout')
@section('title', $post->title)
@section('content')
<a href="{{ route('blog') }}" class="text-green-700 text-sm hover:underline"> Kembali ke Blog</a>
<div class="bg-white rounded-xl shadow p-8 mt-6">
<div class="flex items-center gap-3 mb-3">
<span class="text-xs px-2 py-0.5 rounded-full font-medium
{{ $post->category === 'pengumuman' ? 'bg-yellow-100 text-yellow-700' : ($post->category === 'berita' ? 'bg-blue-100 text-blue-700' : 'bg-gray-100 text-gray-600') }}">
{{ ucfirst($post->category) }}
</span>
<span class="text-xs text-gray-400">{{ $post->published_at->format('d M Y') }}</span>
</div>
<h1 class="text-3xl font-bold mb-2">{{ $post->title }}</h1>
<p class="text-sm text-gray-400 mb-8">Oleh {{ $post->author->name }}</p>
<div class="prose max-w-none text-gray-700 leading-relaxed">
{!! $post->content !!}
</div>
</div>
@endsection
+31
View File
@@ -0,0 +1,31 @@
@extends('public.layout')
@section('title', 'Blog')
@section('content')
<h1 class="text-3xl font-bold mb-2">Blog</h1>
<p class="text-gray-500 mb-10">Artikel, pengumuman, dan berita dari Persegi</p>
<div class="grid md:grid-cols-3 gap-6">
@forelse($posts as $post)
<a href="{{ route('blog.detail', $post) }}" class="bg-white rounded-xl shadow hover:shadow-md transition p-5 block">
<div class="flex items-center gap-2 mb-2">
<span class="text-xs px-2 py-0.5 rounded-full font-medium
{{ $post->category === 'pengumuman' ? 'bg-yellow-100 text-yellow-700' : ($post->category === 'berita' ? 'bg-blue-100 text-blue-700' : 'bg-gray-100 text-gray-600') }}">
{{ ucfirst($post->category) }}
</span>
<span class="text-xs text-gray-400">{{ $post->published_at->format('d M Y') }}</span>
</div>
<h3 class="font-bold text-gray-800 mb-2">{{ $post->title }}</h3>
<p class="text-sm text-gray-500 line-clamp-3">{{ strip_tags($post->content) }}</p>
<div class="text-xs text-gray-400 mt-3">{{ $post->author->name }}</div>
</a>
@empty
<p class="text-gray-400 col-span-3">Belum ada artikel.</p>
@endforelse
</div>
<div class="mt-10">{{ $posts->links() }}</div>
@endsection
+1
View File
@@ -15,6 +15,7 @@
<a href="{{ route('home') }}" class="hover:underline">Beranda</a> <a href="{{ route('home') }}" class="hover:underline">Beranda</a>
<a href="{{ route('tentang') }}" class="hover:underline">Tentang</a> <a href="{{ route('tentang') }}" class="hover:underline">Tentang</a>
<a href="{{ route('kegiatan') }}" class="hover:underline">Kegiatan</a> <a href="{{ route('kegiatan') }}" class="hover:underline">Kegiatan</a>
<a href="{{ route('blog') }}" class="hover:underline">Blog</a>
<a href="{{ route('filament.admin.auth.login') }}" class="bg-white text-green-700 px-3 py-1 rounded hover:bg-green-50">Login</a> <a href="{{ route('filament.admin.auth.login') }}" class="bg-white text-green-700 px-3 py-1 rounded hover:bg-green-50">Login</a>
</div> </div>
</div> </div>
+2
View File
@@ -6,3 +6,5 @@ Route::get('/', [\App\Http\Controllers\PublicController::class, 'home'])->name('
Route::get('/tentang', [\App\Http\Controllers\PublicController::class, 'tentang'])->name('tentang'); Route::get('/tentang', [\App\Http\Controllers\PublicController::class, 'tentang'])->name('tentang');
Route::get('/kegiatan', [\App\Http\Controllers\PublicController::class, 'kegiatan'])->name('kegiatan'); Route::get('/kegiatan', [\App\Http\Controllers\PublicController::class, 'kegiatan'])->name('kegiatan');
Route::get('/kegiatan/{activity}', [\App\Http\Controllers\PublicController::class, 'kegiatanDetail'])->name('kegiatan.detail'); Route::get('/kegiatan/{activity}', [\App\Http\Controllers\PublicController::class, 'kegiatanDetail'])->name('kegiatan.detail');
Route::get('/blog', [\App\Http\Controllers\PublicController::class, 'blog'])->name('blog');
Route::get('/blog/{post:slug}', [\App\Http\Controllers\PublicController::class, 'blogDetail'])->name('blog.detail');