Skip to content

Commit

Permalink
Sortable Admin Awards (with User Details) (#1701)
Browse files Browse the repository at this point in the history
* Model, Controller, Blade changes

sortable results with award users display

* StyleCI Fix

---------

Co-authored-by: Nabeel S <nabeelio@users.noreply.github.com>
  • Loading branch information
FatihKoz and nabeelio authored Dec 3, 2023
1 parent 4ef86af commit b1ac0e0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 5 deletions.
13 changes: 12 additions & 1 deletion app/Http/Controllers/Admin/AwardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
]);
}

Expand Down Expand Up @@ -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,
]);
}

Expand Down
2 changes: 2 additions & 0 deletions app/Models/Award.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class Award extends Model
'id',
'name',
'description',
'active',
'created_at',
];

/**
Expand Down
9 changes: 9 additions & 0 deletions app/Models/UserAward.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -21,6 +24,12 @@ class UserAward extends Model
'created' => AwardAwarded::class,
];

public $sortable = [
'award_id',
'user_id',
'created_at',
];

/**
* Relationships
*/
Expand Down
5 changes: 5 additions & 0 deletions resources/views/admin/awards/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@
{{ Form::close() }}
</div>
</div>
@if(filled($owners))
<div class="card border-blue-bottom">
@include('admin.awards.owners_table')
</div>
@endif
@endsection
@include('admin.awards.scripts')
22 changes: 22 additions & 0 deletions resources/views/admin/awards/owners_table.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<table class="table table-striped table-responsive" id="owners-table">
<thead>
<th>@sortablelink('user.name', 'User')</th>
<th>@sortablelink('created_at', 'Issued At')</th>
<th>&nbsp;</th>
</thead>
<tbody>
@foreach($owners as $ow)
<tr>
<td>
@if(filled($ow->user))
<a href="{{ route('admin.users.edit', [$ow->user->id]) }}">{{ $ow->user->name_private.' ('.$ow->user->ident.')' }}</a>
@else
Deleted User
@endif
</td>
<td>{{ $ow->created_at->format('d.M.Y H:i') }}</td>
<td>&nbsp;</td>
</tr>
@endforeach
</tbody>
</table>
10 changes: 6 additions & 4 deletions resources/views/admin/awards/table.blade.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<table class="table table-hover table-responsive" id="awards-table">
<thead>
<th>Name</th>
<th>Description</th>
<th>@sortablelink('name', 'Name')</th>
<th>@sortablelink('description', 'Description')</th>
<th>Image</th>
<th class="text-center">Active</th>
<th class="text-center">Owners</th>
<th class="text-center">@sortablelink('active', 'Active')</th>
<th class="text-right">Action</th>
</thead>
<tbody>
@foreach($awards->sortby('name', SORT_NATURAL) as $award)
@foreach($awards as $award)
<tr>
<td>
<a href="{{ route('admin.awards.edit', [$award->id]) }}">{{ $award->name }}</a>
Expand All @@ -22,6 +23,7 @@
-
@endif
</td>
<td class="text-center">{{ $counts[$award->id] }}</td>
<td class="text-center">
@if($award->active)
<i class="fas fa-check-circle fa-2x text-success"></i>
Expand Down

0 comments on commit b1ac0e0

Please sign in to comment.