Skip to content

Commit b8525c2

Browse files
committed
Add Comment feature
1 parent 2b7709a commit b8525c2

File tree

6 files changed

+215
-0
lines changed

6 files changed

+215
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace App\Filament\Resources;
4+
5+
use App\Filament\Resources\CommentResource\Pages;
6+
use App\Filament\Resources\CommentResource\RelationManagers;
7+
use App\Models\Comment;
8+
use Filament\Forms;
9+
use Filament\Resources\Form;
10+
use Filament\Resources\Resource;
11+
use Filament\Resources\Table;
12+
use Filament\Tables;
13+
use Illuminate\Database\Eloquent\Builder;
14+
use Illuminate\Database\Eloquent\SoftDeletingScope;
15+
16+
class CommentResource extends Resource
17+
{
18+
protected static ?string $model = Comment::class;
19+
20+
protected static ?string $navigationIcon = 'heroicon-o-chat';
21+
22+
protected static ?string $navigationGroup = 'Blog';
23+
24+
protected static ?int $navigationSort = 4;
25+
26+
protected static ?string $recordTitleAttribute = 'content';
27+
28+
29+
protected static function getNavigationBadge(): ?string
30+
{
31+
return static::getModel()::count();
32+
}
33+
34+
public static function form(Form $form): Form
35+
{
36+
return $form
37+
->schema([
38+
Forms\Components\TextInput::make('content')
39+
->required()
40+
->maxLength(255),
41+
// Forms\Components\Select::make('post')
42+
// ->translateLabel()
43+
// ->required()
44+
// ->multiple()
45+
// ->relationship('post', 'title')
46+
// ->preload()
47+
// ->searchable(),
48+
49+
]);
50+
}
51+
52+
public static function table(Table $table): Table
53+
{
54+
return $table
55+
->columns([
56+
Tables\Columns\TextColumn::make('post_id'),
57+
Tables\Columns\TextColumn::make('content'),
58+
Tables\Columns\TextColumn::make('created_at')
59+
->dateTime(),
60+
Tables\Columns\TextColumn::make('updated_at')
61+
->dateTime(),
62+
Tables\Columns\TextColumn::make('deleted_at')
63+
->dateTime(),
64+
])
65+
->filters([
66+
Tables\Filters\TrashedFilter::make(),
67+
])
68+
->actions([
69+
Tables\Actions\EditAction::make(),
70+
Tables\Actions\DeleteAction::make(),
71+
Tables\Actions\ForceDeleteAction::make(),
72+
Tables\Actions\RestoreAction::make(),
73+
])
74+
->bulkActions([
75+
Tables\Actions\DeleteBulkAction::make(),
76+
Tables\Actions\ForceDeleteBulkAction::make(),
77+
Tables\Actions\RestoreBulkAction::make(),
78+
]);
79+
}
80+
81+
public static function getPages(): array
82+
{
83+
return [
84+
'index' => Pages\ManageComments::route('/'),
85+
];
86+
}
87+
88+
public static function getEloquentQuery(): Builder
89+
{
90+
return parent::getEloquentQuery()
91+
->withoutGlobalScopes([
92+
SoftDeletingScope::class,
93+
]);
94+
}
95+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace App\Filament\Resources\CommentResource\Pages;
4+
5+
use App\Filament\Resources\CommentResource;
6+
use App\Filament\Resources\CommentResource\Widgets\StatsOverview;
7+
use Filament\Pages\Actions;
8+
use Filament\Resources\Pages\ManageRecords;
9+
10+
class ManageComments extends ManageRecords
11+
{
12+
protected static string $resource = CommentResource::class;
13+
14+
protected function getActions(): array
15+
{
16+
return [
17+
Actions\CreateAction::make(),
18+
];
19+
}
20+
21+
public static function getWidgets(): array
22+
{
23+
return [
24+
StatsOverview::class,
25+
];
26+
}
27+
protected function getHeaderWidgets(): array
28+
{
29+
return [
30+
StatsOverview::class,
31+
];
32+
}
33+
public function updated($name)
34+
{
35+
if (Str::of($name)->contains(['mountedTableAction', 'mountedTableBulkAction'])) {
36+
$this->emit('updateCommentStatsOverview');
37+
}
38+
}
39+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Filament\Resources\CommentResource\Widgets;
4+
5+
use App\Models\Comment;
6+
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
7+
use Filament\Widgets\StatsOverviewWidget\Card;
8+
use Illuminate\Support\Facades\DB;
9+
10+
class StatsOverview extends BaseWidget
11+
{
12+
protected $listeners = ['updateCommentStatsOverview' => '$refresh'];
13+
14+
protected function getCards(): array
15+
{
16+
$comments = Comment::select(DB::raw('count(*) as total '))->first();
17+
18+
return [
19+
Card::make(__('Total'), $comments->total)
20+
->description(__('Total comments'))
21+
->descriptionIcon('heroicon-s-trending-up')
22+
->color('primary'),
23+
];
24+
}
25+
}

app/Models/Comment.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use App\Models\Post;
6+
use Illuminate\Database\Eloquent\Factories\HasFactory;
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\SoftDeletes;
9+
10+
class Comment extends Model
11+
{
12+
use HasFactory, SoftDeletes;
13+
14+
protected $fillable = ['content', 'post_id'];
15+
16+
public function post()
17+
{
18+
return $this->belongsTo(Post::class);
19+
}
20+
}

app/Models/Post.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Models;
44

5+
use App\Models\Comment;
56
use Illuminate\Database\Eloquent\Factories\HasFactory;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -32,4 +33,9 @@ public function tags()
3233
{
3334
return $this->belongsToMany(Tag::class);
3435
}
36+
37+
public function comments()
38+
{
39+
return $this->hasMany(Comment::class);
40+
}
3541
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('comments', function (Blueprint $table) {
15+
$table->id();
16+
$table->foreignId('post_id')->constrained()->onDelete('cascade');
17+
$table->string('content');
18+
$table->timestamps();
19+
$table->softDeletes();
20+
});
21+
}
22+
23+
/**
24+
* Reverse the migrations.
25+
*/
26+
public function down(): void
27+
{
28+
Schema::dropIfExists('comments');
29+
}
30+
};

0 commit comments

Comments
 (0)