From b1ac0e04f90a0a45fff72b2f65136561b810ba47 Mon Sep 17 00:00:00 2001 From: "B.Fatih KOZ" Date: Sun, 3 Dec 2023 20:32:50 +0300 Subject: [PATCH] Sortable Admin Awards (with User Details) (#1701) * Model, Controller, Blade changes sortable results with award users display * StyleCI Fix --------- Co-authored-by: Nabeel S --- .../Controllers/Admin/AwardController.php | 13 ++++++++++- app/Models/Award.php | 2 ++ app/Models/UserAward.php | 9 ++++++++ resources/views/admin/awards/edit.blade.php | 5 +++++ .../views/admin/awards/owners_table.blade.php | 22 +++++++++++++++++++ resources/views/admin/awards/table.blade.php | 10 +++++---- 6 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 resources/views/admin/awards/owners_table.blade.php diff --git a/app/Http/Controllers/Admin/AwardController.php b/app/Http/Controllers/Admin/AwardController.php index b8a8b7fbf..2d80bfebc 100755 --- a/app/Http/Controllers/Admin/AwardController.php +++ b/app/Http/Controllers/Admin/AwardController.php @@ -5,6 +5,7 @@ use App\Contracts\Controller; use App\Http\Requests\CreateAwardRequest; use App\Http\Requests\UpdateAwardRequest; +use App\Models\UserAward; use App\Repositories\AwardRepository; use App\Services\AwardService; use Illuminate\Http\RedirectResponse; @@ -62,10 +63,17 @@ protected function getAwardClassesAndDescriptions(): array public function index(Request $request): View { $this->awardRepo->pushCriteria(new RequestCriteria($request)); - $awards = $this->awardRepo->all(); + $awards = $this->awardRepo->sortable('name')->get(); + + $counts = []; + + foreach ($awards as $aw) { + $counts[$aw->id] = UserAward::where('award_id', $aw->id)->count(); + } return view('admin.awards.index', [ 'awards' => $awards, + 'counts' => $counts, ]); } @@ -139,10 +147,13 @@ public function edit(int $id): RedirectResponse|View $class_refs = $this->getAwardClassesAndDescriptions(); + $owners = UserAward::with('user')->where('award_id', $id)->sortable(['created_at' => 'desc'])->get(); + return view('admin.awards.edit', [ 'award' => $award, 'award_classes' => $class_refs['awards'], 'award_descriptions' => $class_refs['descriptions'], + 'owners' => $owners, ]); } diff --git a/app/Models/Award.php b/app/Models/Award.php index d4a3a656b..22559d8b5 100755 --- a/app/Models/Award.php +++ b/app/Models/Award.php @@ -48,6 +48,8 @@ class Award extends Model 'id', 'name', 'description', + 'active', + 'created_at', ]; /** diff --git a/app/Models/UserAward.php b/app/Models/UserAward.php index 26ca17fe9..dafeb0373 100644 --- a/app/Models/UserAward.php +++ b/app/Models/UserAward.php @@ -6,10 +6,13 @@ use App\Events\AwardAwarded; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Notifications\Notifiable; +use Kyslik\ColumnSortable\Sortable; class UserAward extends Model { use Notifiable; + use Sortable; + public $table = 'user_awards'; protected $fillable = [ @@ -21,6 +24,12 @@ class UserAward extends Model 'created' => AwardAwarded::class, ]; + public $sortable = [ + 'award_id', + 'user_id', + 'created_at', + ]; + /** * Relationships */ diff --git a/resources/views/admin/awards/edit.blade.php b/resources/views/admin/awards/edit.blade.php index c27eb54af..9a578c807 100755 --- a/resources/views/admin/awards/edit.blade.php +++ b/resources/views/admin/awards/edit.blade.php @@ -8,5 +8,10 @@ {{ Form::close() }} + @if(filled($owners)) +
+ @include('admin.awards.owners_table') +
+ @endif @endsection @include('admin.awards.scripts') diff --git a/resources/views/admin/awards/owners_table.blade.php b/resources/views/admin/awards/owners_table.blade.php new file mode 100644 index 000000000..b61516eff --- /dev/null +++ b/resources/views/admin/awards/owners_table.blade.php @@ -0,0 +1,22 @@ + + + + + + + + @foreach($owners as $ow) + + + + + + @endforeach + +
@sortablelink('user.name', 'User')@sortablelink('created_at', 'Issued At') 
+ @if(filled($ow->user)) + {{ $ow->user->name_private.' ('.$ow->user->ident.')' }} + @else + Deleted User + @endif + {{ $ow->created_at->format('d.M.Y H:i') }} 
\ No newline at end of file diff --git a/resources/views/admin/awards/table.blade.php b/resources/views/admin/awards/table.blade.php index 9e33a2d9e..17bd21f96 100755 --- a/resources/views/admin/awards/table.blade.php +++ b/resources/views/admin/awards/table.blade.php @@ -1,13 +1,14 @@ - - + + - + + - @foreach($awards->sortby('name', SORT_NATURAL) as $award) + @foreach($awards as $award) +
NameDescription@sortablelink('name', 'Name')@sortablelink('description', 'Description') ImageActiveOwners@sortablelink('active', 'Active') Action
{{ $award->name }} @@ -22,6 +23,7 @@ - @endif {{ $counts[$award->id] }} @if($award->active)