From 74fc3b5e6aaeb28efc6c35fb9402dad73189c902 Mon Sep 17 00:00:00 2001 From: Nurmuhammet Allanov Date: Mon, 28 Jul 2025 18:56:17 +0500 Subject: [PATCH] Implement news management features: add author field and comments functionality in NewsResource, enhance HomePageController to fetch latest news, and create dedicated views for news display and commenting. Update routes for comment submission and adjust homepage to showcase recent news articles. --- app/Filament/Pages/HomePageSettings.php | 4 +- app/Filament/Pages/ManageCtaSettings.php | 40 ++++++++ app/Filament/Resources/CommentResource.php | 99 +++++++++++++++++++ .../CommentResource/Pages/CreateComment.php | 12 +++ .../CommentResource/Pages/EditComment.php | 19 ++++ .../CommentResource/Pages/ListComments.php | 19 ++++ app/Filament/Resources/NewsResource.php | 10 +- .../CommentsRelationManager.php | 64 ++++++++++++ app/Http/Controllers/HomePageController.php | 4 +- app/Http/Controllers/NewsPageController.php | 15 +++ app/Models/Comment.php | 24 +++++ app/Models/News.php | 7 ++ app/Providers/Filament/PanelPanelProvider.php | 6 +- app/Settings/CtaSettings.php | 18 ++++ ..._07_28_183714_add_author_to_news_table.php | 28 ++++++ ...025_07_28_183737_create_comments_table.php | 31 ++++++ .../2025_07_28_184818_create_cta_settings.php | 14 +++ database/seeders/NewsTableSeeder.php | 4 +- .../views/web/pages/home/index.blade.php | 63 +++--------- .../views/web/pages/news/index.blade.php | 2 +- resources/views/web/pages/news/show.blade.php | 87 ++++++++++++++++ routes/web.php | 1 + 22 files changed, 513 insertions(+), 58 deletions(-) create mode 100644 app/Filament/Pages/ManageCtaSettings.php create mode 100644 app/Filament/Resources/CommentResource.php create mode 100644 app/Filament/Resources/CommentResource/Pages/CreateComment.php create mode 100644 app/Filament/Resources/CommentResource/Pages/EditComment.php create mode 100644 app/Filament/Resources/CommentResource/Pages/ListComments.php create mode 100644 app/Filament/Resources/NewsResource/RelationManagers/CommentsRelationManager.php create mode 100644 app/Models/Comment.php create mode 100644 app/Settings/CtaSettings.php create mode 100644 database/migrations/2025_07_28_183714_add_author_to_news_table.php create mode 100644 database/migrations/2025_07_28_183737_create_comments_table.php create mode 100644 database/migrations/2025_07_28_184818_create_cta_settings.php diff --git a/app/Filament/Pages/HomePageSettings.php b/app/Filament/Pages/HomePageSettings.php index e028d81..d4af328 100644 --- a/app/Filament/Pages/HomePageSettings.php +++ b/app/Filament/Pages/HomePageSettings.php @@ -15,8 +15,6 @@ use Illuminate\Contracts\Support\Htmlable; class HomePageSettings extends SettingsPage { - protected static ?string $navigationIcon = 'heroicon-o-home'; - protected static string $settings = HomeSettings::class; public function form(Form $form): Form @@ -291,4 +289,4 @@ class HomePageSettings extends SettingsPage { return 'Manage the homepage hero section, background video, and call-to-action content.'; } -} +} \ No newline at end of file diff --git a/app/Filament/Pages/ManageCtaSettings.php b/app/Filament/Pages/ManageCtaSettings.php new file mode 100644 index 0000000..d9c4233 --- /dev/null +++ b/app/Filament/Pages/ManageCtaSettings.php @@ -0,0 +1,40 @@ +schema([ + Forms\Components\TextInput::make('title') + ->label('Call To Action Title') + ->required(), + Forms\Components\TextInput::make('button_text') + ->label('Button Text') + ->required(), + Forms\Components\TextInput::make('button_url') + ->label('Button URL') + ->required() + ->url(), + FileUpload::make('background_image') + ->label('Background Image 1320x408') + ->directory('settings') + ->image() + ->columnSpan('full'), + ]); + } +} \ No newline at end of file diff --git a/app/Filament/Resources/CommentResource.php b/app/Filament/Resources/CommentResource.php new file mode 100644 index 0000000..d08d5fc --- /dev/null +++ b/app/Filament/Resources/CommentResource.php @@ -0,0 +1,99 @@ +schema([ + Forms\Components\Card::make() + ->schema([ + Select::make('news_id') + ->relationship('news', 'title') + ->required(), + TextInput::make('title') + ->required() + ->maxLength(255), + RichEditor::make('message') + ->required() + ->columnSpanFull(), + TextInput::make('author_name') + ->label('Author Name (Optional)') + ->maxLength(255), + ]) + ->columns(2), + ]); + } + + public static function table(Table $table): Table + { + return $table + ->columns([ + Tables\Columns\TextColumn::make('news.title') + ->label('News Article') + ->searchable() + ->sortable(), + Tables\Columns\TextColumn::make('title') + ->searchable() + ->sortable(), + Tables\Columns\TextColumn::make('author_name') + ->label('Author') + ->searchable() + ->sortable(), + Tables\Columns\TextColumn::make('created_at') + ->dateTime() + ->sortable() + ->toggleable(isToggledHiddenByDefault: true), + ]) + ->filters([ + // + ]) + ->actions([ + Tables\Actions\EditAction::make(), + Tables\Actions\DeleteAction::make(), + ]) + ->bulkActions([ + Tables\Actions\BulkActionGroup::make([ + Tables\Actions\DeleteBulkAction::make(), + ]), + ]); + } + + public static function getRelations(): array + { + return [ + // + ]; + } + + public static function getPages(): array + { + return [ + 'index' => Pages\ListComments::route('/'), + 'create' => Pages\CreateComment::route('/create'), + 'edit' => Pages\EditComment::route('/{record}/edit'), + ]; + } +} diff --git a/app/Filament/Resources/CommentResource/Pages/CreateComment.php b/app/Filament/Resources/CommentResource/Pages/CreateComment.php new file mode 100644 index 0000000..b15f825 --- /dev/null +++ b/app/Filament/Resources/CommentResource/Pages/CreateComment.php @@ -0,0 +1,12 @@ +dehydrated() ->unique(News::class, 'slug', ignoreRecord: true), FileUpload::make('image') + ->label('Image 1100x660') ->image() ->directory('news') ->nullable() ->columnSpanFull(), + TextInput::make('author') + ->required() + ->maxLength(255), RichEditor::make('content') ->required() ->columnSpanFull(), @@ -70,6 +75,9 @@ class NewsResource extends Resource Tables\Columns\TextColumn::make('slug') ->searchable() ->sortable(), + Tables\Columns\TextColumn::make('author') + ->searchable() + ->sortable(), Tables\Columns\TextColumn::make('published_at') ->dateTime() ->sortable(), @@ -99,7 +107,7 @@ class NewsResource extends Resource public static function getRelations(): array { return [ - // + CommentsRelationManager::class, ]; } diff --git a/app/Filament/Resources/NewsResource/RelationManagers/CommentsRelationManager.php b/app/Filament/Resources/NewsResource/RelationManagers/CommentsRelationManager.php new file mode 100644 index 0000000..10c7cfc --- /dev/null +++ b/app/Filament/Resources/NewsResource/RelationManagers/CommentsRelationManager.php @@ -0,0 +1,64 @@ +schema([ + TextInput::make('title') + ->required() + ->maxLength(255), + RichEditor::make('message') + ->required() + ->columnSpanFull(), + TextInput::make('author_name') + ->label('Author Name (Optional)') + ->maxLength(255), + ]); + } + + public function table(Table $table): Table + { + return $table + ->recordTitleAttribute('title') + ->columns([ + Tables\Columns\TextColumn::make('title') + ->searchable(), + Tables\Columns\TextColumn::make('author_name') + ->label('Author') + ->searchable(), + Tables\Columns\TextColumn::make('created_at') + ->dateTime() + ->sortable() + ->toggleable(isToggledHiddenByDefault: true), + ]) + ->filters([ + // + ]) + ->headerActions([ + Tables\Actions\CreateAction::make(), + ]) + ->actions([ + Tables\Actions\EditAction::make(), + Tables\Actions\DeleteAction::make(), + ]) + ->bulkActions([ + Tables\Actions\BulkActionGroup::make([ + Tables\Actions\DeleteBulkAction::make(), + ]), + ]); + } +} \ No newline at end of file diff --git a/app/Http/Controllers/HomePageController.php b/app/Http/Controllers/HomePageController.php index 4415f53..1231931 100644 --- a/app/Http/Controllers/HomePageController.php +++ b/app/Http/Controllers/HomePageController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Models\Brand; +use App\Models\News; use App\Settings\HomeSettings; class HomePageController extends Controller @@ -11,7 +12,8 @@ class HomePageController extends Controller { $brands = Brand::query()->get(['id', 'image']); $homeSettings = app(HomeSettings::class); + $allNews = News::query()->latest()->take(3)->get(); - return view('web.pages.home.index', compact('homeSettings', 'brands')); + return view('web.pages.home.index', compact('homeSettings', 'brands', 'allNews')); } } diff --git a/app/Http/Controllers/NewsPageController.php b/app/Http/Controllers/NewsPageController.php index 2e93cbf..e55eeb7 100644 --- a/app/Http/Controllers/NewsPageController.php +++ b/app/Http/Controllers/NewsPageController.php @@ -3,6 +3,8 @@ namespace App\Http\Controllers; use App\Models\News; +use App\Models\Comment; +use Illuminate\Http\Request; class NewsPageController extends Controller { @@ -17,4 +19,17 @@ class NewsPageController extends Controller { return view('web.pages.news.show', compact('news')); } + + public function storeComment(Request $request, News $news) + { + $validated = $request->validate([ + 'title' => 'required|string|max:255', + 'message' => 'required|string', + 'author_name' => 'nullable|string|max:255', + ]); + + $news->comments()->create($validated); + + return back()->with('success', 'Comment added successfully!'); + } } diff --git a/app/Models/Comment.php b/app/Models/Comment.php new file mode 100644 index 0000000..f4e0d00 --- /dev/null +++ b/app/Models/Comment.php @@ -0,0 +1,24 @@ +belongsTo(News::class); + } +} diff --git a/app/Models/News.php b/app/Models/News.php index d9f6270..8b8a0fc 100644 --- a/app/Models/News.php +++ b/app/Models/News.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasMany; class News extends Model { @@ -15,5 +16,11 @@ class News extends Model 'content', 'image', 'published_at', + 'author', ]; + + public function comments(): HasMany + { + return $this->hasMany(Comment::class); + } } diff --git a/app/Providers/Filament/PanelPanelProvider.php b/app/Providers/Filament/PanelPanelProvider.php index 9444178..dbb45e9 100644 --- a/app/Providers/Filament/PanelPanelProvider.php +++ b/app/Providers/Filament/PanelPanelProvider.php @@ -3,7 +3,6 @@ namespace App\Providers\Filament; use Filament\Http\Middleware\Authenticate; -use Filament\Http\Middleware\AuthenticateSession; use Filament\Http\Middleware\DisableBladeIconComponents; use Filament\Http\Middleware\DispatchServingFilamentEvent; use Filament\Pages; @@ -15,8 +14,11 @@ use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse; use Illuminate\Cookie\Middleware\EncryptCookies; use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken; use Illuminate\Routing\Middleware\SubstituteBindings; +use Illuminate\Session\Middleware\AuthenticateSession; use Illuminate\Session\Middleware\StartSession; use Illuminate\View\Middleware\ShareErrorsFromSession; +use App\Filament\Pages\HomePageSettings; +use App\Filament\Pages\ManageCtaSettings; class PanelPanelProvider extends PanelProvider { @@ -34,6 +36,8 @@ class PanelPanelProvider extends PanelProvider ->discoverPages(in: app_path('Filament/Pages'), for: 'App\\Filament\\Pages') ->pages([ Pages\Dashboard::class, + HomePageSettings::class, + ManageCtaSettings::class, ]) ->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets') ->widgets([ diff --git a/app/Settings/CtaSettings.php b/app/Settings/CtaSettings.php new file mode 100644 index 0000000..54a49d8 --- /dev/null +++ b/app/Settings/CtaSettings.php @@ -0,0 +1,18 @@ +string('author')->after('image')->nullable(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('news', function (Blueprint $table) { + $table->dropColumn('author'); + }); + } +}; diff --git a/database/migrations/2025_07_28_183737_create_comments_table.php b/database/migrations/2025_07_28_183737_create_comments_table.php new file mode 100644 index 0000000..c3dee66 --- /dev/null +++ b/database/migrations/2025_07_28_183737_create_comments_table.php @@ -0,0 +1,31 @@ +id(); + $table->string('title'); + $table->text('message'); + $table->string('author_name')->nullable(); + $table->foreignId('news_id')->constrained()->onDelete('cascade'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('comments'); + } +}; diff --git a/database/migrations/2025_07_28_184818_create_cta_settings.php b/database/migrations/2025_07_28_184818_create_cta_settings.php new file mode 100644 index 0000000..b727c8c --- /dev/null +++ b/database/migrations/2025_07_28_184818_create_cta_settings.php @@ -0,0 +1,14 @@ +migrator->add('cta.title', 'Join Our Community and Access Exclusive Insights Today'); + $this->migrator->add('cta.button_text', 'Free Consultation'); + $this->migrator->add('cta.button_url', 'request-quote.html'); + $this->migrator->add('cta.background_image', ''); + } +}; diff --git a/database/seeders/NewsTableSeeder.php b/database/seeders/NewsTableSeeder.php index d700987..5aafe35 100644 --- a/database/seeders/NewsTableSeeder.php +++ b/database/seeders/NewsTableSeeder.php @@ -13,6 +13,8 @@ class NewsTableSeeder extends Seeder */ public function run(): void { - News::factory()->count(12)->create(); + News::factory()->count(12)->create([ + 'image' => '/web/assets/img/page/breadcrumb.jpg', + ]); } } \ No newline at end of file diff --git a/resources/views/web/pages/home/index.blade.php b/resources/views/web/pages/home/index.blade.php index 8390386..4234980 100644 --- a/resources/views/web/pages/home/index.blade.php +++ b/resources/views/web/pages/home/index.blade.php @@ -3,6 +3,7 @@ @inject('solutionSettings', 'App\Settings\SolutionSettings') @inject('successSettings', 'App\Settings\SuccessSettings') @inject('portfolioSettings', 'App\Settings\PortfolioSettings') +@inject('ctaSettings', 'App\Settings\CtaSettings') @section('content') @@ -315,71 +316,33 @@
News & Blog -

Update News & Blogs

+

News

+ @foreach($allNews as $news)
- image + image
-
22
-
Dec
+
{{ \Carbon\Carbon::parse($news->published_at)->format('d') }}
+
{{ \Carbon\Carbon::parse($news->published_at)->format('M') }}
-
-
-
-
-
- image -
-
19
-
Dec
-
-
- -
-
- + @endforeach
@@ -390,11 +353,11 @@
-
+
-

Join Our Community and Access Exclusive Insights Today

+

{{ $ctaSettings->title }}

diff --git a/resources/views/web/pages/news/index.blade.php b/resources/views/web/pages/news/index.blade.php index d4ceb58..86f00ee 100644 --- a/resources/views/web/pages/news/index.blade.php +++ b/resources/views/web/pages/news/index.blade.php @@ -2,7 +2,7 @@ @section('content') -