-
-
Notifications
You must be signed in to change notification settings - Fork 78
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
Add devblog #179
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7d073ef
add blog to menu's
gwleuverink 2234621
add article model & factory
gwleuverink 975a805
wire up article listing & pagination
gwleuverink 7d5a86f
wire up article detail
gwleuverink 802ebbe
add published_at guards
gwleuverink 7bf7b15
add article scheduling tests
gwleuverink 9136db1
wip - tidy scope
gwleuverink 9b62780
add filament resource
gwleuverink 007090b
add dark mode styles for prose elements
gwleuverink 00f083d
add publish & schedule actions
gwleuverink f0efe11
tidy - improve filament article sorting
gwleuverink daa961c
wip - validation
gwleuverink beb8a4d
add unpublish action
gwleuverink 53c3927
add preview action and update user admin check
gwleuverink 1afbdf9
add article preview tests
gwleuverink 4f50cf9
update auto-slug logic
gwleuverink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()), | ||
gwleuverink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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
16
app/Filament/Resources/ArticleResource/Pages/CreateArticle.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
app/Filament/Resources/ArticleResource/Pages/EditArticle.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
gwleuverink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
]; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/Filament/Resources/ArticleResource/Pages/ListArticles.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
gwleuverink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return view('article', [ | ||
'article' => $article, | ||
]); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
database/migrations/2025_07_08_083141_create_articles_table.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.