Skip to content

Add devblog #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions app/Filament/Resources/ArticleResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ArticleResource\Pages;
use App\Models\Article;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\MarkdownEditor;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Forms\Set;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Support\Str;

class ArticleResource extends Resource
{
protected static ?string $model = Article::class;

protected static ?string $recordRouteKeyName = 'id';

protected static ?string $recordTitleAttribute = 'title';

protected static ?string $navigationIcon = 'heroicon-o-newspaper';

public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('title')
->required()
->maxLength(255),

TextInput::make('slug')
->required()
->maxLength(255)
->live(onBlur: true)
->unique(Article::class, 'slug', ignoreRecord: true)
->afterStateUpdated(fn (Set $set, ?string $state) => $set('slug', Str::slug($state))),

DateTimePicker::make('published_at')
->label('Published At')
->displayFormat('M j, Y H:i')
->seconds(false)
->dehydrated()
->reactive()
->default(now()),

Textarea::make('excerpt')
->required()
->maxLength(400)
->columnSpanFull(),

MarkdownEditor::make('content')
->required()
->columnSpanFull(),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('title')
->searchable()
->sortable(),

TextColumn::make('excerpt')
->searchable()
->limit(50),

TextColumn::make('author.name')
->label('Author')
->searchable()
->sortable(),
TextColumn::make('published_at')
->dateTime('M j, Y H:i')
->sortable()
->badge()
->color(fn ($state) => $state && $state->isPast() ? 'success' : 'warning'),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make()
->url(fn ($record) => static::getUrl('edit', ['record' => $record->id])),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
])
->defaultSort('published_at', 'desc');
}

public static function getRelations(): array
{
return [
//
];
}

public static function getPages(): array
{
return [
'index' => Pages\ListArticles::route('/'),
'create' => Pages\CreateArticle::route('/create'),
'edit' => Pages\EditArticle::route('/{record}/edit'),
];
}
}
16 changes: 16 additions & 0 deletions app/Filament/Resources/ArticleResource/Pages/CreateArticle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Filament\Resources\ArticleResource\Pages;

use App\Filament\Resources\ArticleResource;
use Filament\Resources\Pages\CreateRecord;

class CreateArticle extends CreateRecord
{
protected static string $resource = ArticleResource::class;

protected function getRedirectUrl(): string
{
return static::getResource()::getUrl('edit', ['record' => $this->record->id]);
}
}
19 changes: 19 additions & 0 deletions app/Filament/Resources/ArticleResource/Pages/EditArticle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\ArticleResource\Pages;

use App\Filament\Resources\ArticleResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;

class EditArticle extends EditRecord
{
protected static string $resource = ArticleResource::class;

protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
}
19 changes: 19 additions & 0 deletions app/Filament/Resources/ArticleResource/Pages/ListArticles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\ArticleResource\Pages;

use App\Filament\Resources\ArticleResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;

class ListArticles extends ListRecords
{
protected static string $resource = ArticleResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
28 changes: 28 additions & 0 deletions app/Http/Controllers/ShowBlogController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Controllers;

use App\Models\Article;

class ShowBlogController extends Controller
{
public function index()
{
$articles = Article::query()
->published()
->paginate(6);

return view('blog', [
'articles' => $articles,
]);
}

public function show(Article $article)
{
abort_if($article->published_at->isFuture(), 404);

return view('article', [
'article' => $article,
]);
}
}
54 changes: 54 additions & 0 deletions app/Models/Article.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Models;

use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Article extends Model
{
use HasFactory;

protected $fillable = [
'slug',
'title',
'excerpt',
'content',
'published_at',
];

protected $casts = [
'published_at' => 'datetime',
];

public function getRouteKeyName()
{
return 'slug';
}

public function scopePublished(Builder $query): void
{
$query
->orderByDesc('published_at')
->whereNotNull('published_at')
->where('published_at', '<=', now());
}

public function author(): BelongsTo
{
return $this->belongsTo(User::class, 'author_id');
}

protected static function boot()
{
parent::boot();

static::creating(function ($article) {
if (auth()->check() && ! $article->author_id) {
$article->author_id = auth()->id();
}
});
}
}
38 changes: 38 additions & 0 deletions database/factories/ArticleFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Database\Factories;

use App\Models\Article;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends Factory<Article>
*/
class ArticleFactory extends Factory
{
public function definition(): array
{
return [
'author_id' => User::factory(),
'slug' => fake()->unique()->slug(3),
'title' => fake()->sentence(),
'excerpt' => fake()->paragraph(1, false),
'content' => implode(PHP_EOL.PHP_EOL, fake()->paragraphs()),
];
}

public function published(): static
{
return $this->state(fn () => [
'published_at' => now(),
]);
}

public function scheduled(): static
{
return $this->state(fn () => [
'published_at' => now()->addMinute(),
]);
}
}
41 changes: 41 additions & 0 deletions database/migrations/2025_07_08_083141_create_articles_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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('articles', function (Blueprint $table) {
$table->id();

$table->unsignedBigInteger('author_id')
->nullable();

$table->foreign('author_id')
->references('id')->on('users')
->nullOnDelete();

$table->string('slug')->unique();
$table->string('title', 255);
$table->string('excerpt', 400);
$table->text('content');

$table->timestamp('published_at')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('articles');
}
};
Loading