From 9e7fda4ea2b7841a4fbb0fec9f95b1a2b088e433 Mon Sep 17 00:00:00 2001 From: kusowl Date: Thu, 22 Jan 2026 16:36:22 +0530 Subject: [PATCH] feature (stats in broker dashboard): show individual brokers total likes, view and click per deal andin stats --- app/Actions/GetBrokerStatsAction.php | 22 +++++++++++++++++++ .../Broker/BrokerDashboardController.php | 9 ++++++-- .../Controllers/InteractionController.php | 2 -- app/Models/Deal.php | 14 ++++++++++++ app/Models/User.php | 6 +++++ .../dashboard/broker/listing-card.blade.php | 3 +-- .../dashboard/broker/listing-stats.blade.php | 4 ++-- .../dashboard/broker/stats.blade.php | 10 ++++----- .../views/dashboards/broker/index.blade.php | 2 +- 9 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 app/Actions/GetBrokerStatsAction.php diff --git a/app/Actions/GetBrokerStatsAction.php b/app/Actions/GetBrokerStatsAction.php new file mode 100644 index 0000000..88421c2 --- /dev/null +++ b/app/Actions/GetBrokerStatsAction.php @@ -0,0 +1,22 @@ + + */ + public function execute(User $user): array + { + return [ + 'listings' => $user->deals()->count(), + 'likes' => $user->dealsInteractions()->where('type', InteractionType::Like)->count(), + 'views' => $user->dealsInteractions()->where('type', InteractionType::View)->sum('count'), + 'clicks' => $user->dealsInteractions()->where('type', InteractionType::Redirection)->sum('count'), + ]; + } +} diff --git a/app/Http/Controllers/Broker/BrokerDashboardController.php b/app/Http/Controllers/Broker/BrokerDashboardController.php index 3424b24..546326b 100644 --- a/app/Http/Controllers/Broker/BrokerDashboardController.php +++ b/app/Http/Controllers/Broker/BrokerDashboardController.php @@ -2,15 +2,17 @@ namespace App\Http\Controllers\Broker; +use App\Actions\GetBrokerStatsAction; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Auth; class BrokerDashboardController extends Controller { - public function index() + public function index(GetBrokerStatsAction $getBrokerStatsAction) { return view('dashboards.broker.index') - ->with('deals', $this->deals()); + ->with('deals', $this->deals()) + ->with('stats', $getBrokerStatsAction->execute(Auth::user())); } protected function deals() @@ -28,6 +30,9 @@ protected function deals() 'deal_category_id', ]) ->with('category:id,name') + ->WithLikePerDeal() + ->WithRedirectionPerDeal() + ->withViewPerDeal() ->latest() ->paginate(); } diff --git a/app/Http/Controllers/InteractionController.php b/app/Http/Controllers/InteractionController.php index 95fdd5d..71d4fe1 100644 --- a/app/Http/Controllers/InteractionController.php +++ b/app/Http/Controllers/InteractionController.php @@ -99,7 +99,6 @@ public function redirect(Deal $deal) public function view(Deal $deal) { - ds('hi'); try { $interaction = $deal->interactions()->firstOrCreate([ 'type' => InteractionType::View, @@ -109,7 +108,6 @@ public function view(Deal $deal) if (! $interaction->wasRecentlyCreated) { $interaction->increment('count'); } - ds($interaction); } catch (\Throwable $e) { Log::error('Error when view a deal', diff --git a/app/Models/Deal.php b/app/Models/Deal.php index ef3c218..5c347f0 100644 --- a/app/Models/Deal.php +++ b/app/Models/Deal.php @@ -86,6 +86,19 @@ public function WithRedirectionPerDeal(Builder $query): Builder ], 'count'); } + /** + * Scope a query to get a view count per deal + */ + #[Scope] + public function withViewPerDeal(Builder $query): Builder + { + return $query->withSum([ + 'interactions as total_views' => function ($query) { + $query->where('type', InteractionType::View); + }, + ], 'count'); + } + /** * Scope a search in a query */ @@ -105,4 +118,5 @@ public function filterByCategory(Builder $query, string $category): Builder { return $query->where('deal_category_id', $category); } + } diff --git a/app/Models/User.php b/app/Models/User.php index 3f67aab..d6ff772 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; @@ -78,4 +79,9 @@ public function recentSearches(): HasMany { return $this->hasMany(RecentSearch::class); } + + public function dealsInteractions(): HasManyThrough + { + return $this->hasManyThrough(Interaction::class, Deal::class); + } } diff --git a/resources/views/components/dashboard/broker/listing-card.blade.php b/resources/views/components/dashboard/broker/listing-card.blade.php index 088c217..c540c35 100644 --- a/resources/views/components/dashboard/broker/listing-card.blade.php +++ b/resources/views/components/dashboard/broker/listing-card.blade.php @@ -1,5 +1,4 @@ @props(['deal']) -
@@ -27,7 +26,7 @@ class="text-xs underline text-accent-601 mt-1">

{{$deal->category->name}}

- +
0, 'likes' => 0, 'clicks' => 0]) +@props(['views' => 0, 'likes' => 0, 'clicks' => 0])
-

{{$impression}}

+

{{$views}}

diff --git a/resources/views/components/dashboard/broker/stats.blade.php b/resources/views/components/dashboard/broker/stats.blade.php index d1439ab..9272c6a 100644 --- a/resources/views/components/dashboard/broker/stats.blade.php +++ b/resources/views/components/dashboard/broker/stats.blade.php @@ -1,22 +1,22 @@ -@props(['list_count' => 0]) +@props(['stats' => []])
- + - + - + - + diff --git a/resources/views/dashboards/broker/index.blade.php b/resources/views/dashboards/broker/index.blade.php index 435ccc1..a28662b 100644 --- a/resources/views/dashboards/broker/index.blade.php +++ b/resources/views/dashboards/broker/index.blade.php @@ -2,6 +2,6 @@ - +