Skip to content

Commit

Permalink
Merge pull request #911 from owlchester/race-overview-character-bug
Browse files Browse the repository at this point in the history
Fixed races character datagrid
  • Loading branch information
ilestis authored Jul 8, 2024
2 parents dd234ca + 946db3f commit e71b474
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
11 changes: 5 additions & 6 deletions app/Http/Controllers/Races/MemberController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ public function index(Campaign $campaign, Race $race)
$this->campaign($campaign)->authEntityView($race->entity);

$options = ['campaign' => $campaign, 'race' => $race];
$filters = [];
$relation = 'characterRaces';
$relation = 'allCharacterRaces';
if (request()->has('race_id')) {
$options['race_id'] = (int) request()->get('race_id');
$filters['race_id'] = $options['race_id'];
$relation = 'characters';
$relation = 'characterRaces';
}
Datagrid::layout(\App\Renderers\Layouts\Race\Character::class)
->route('races.characters', $options);
Expand All @@ -40,12 +38,13 @@ public function index(Campaign $campaign, Race $race)
->with([
'race', 'race.entity',
'character', 'character.entity', 'character.entity.tags', 'character.entity.tags.entity', 'character.entity.image',
'character.location', 'character.location.entity'])
'character.location', 'character.location.entity',
'character.characterRaces'
])
->has('character')
->has('character.entity')
->leftJoin('characters as c', 'c.id', 'character_race.character_id')
->sort(request()->only(['o', 'k']), ['c.name' => 'asc'])
->filter($filters)
->paginate(15);

return $this->campaign($campaign)->datagridAjax();
Expand Down
10 changes: 10 additions & 0 deletions app/Models/CharacterRace.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class CharacterRace extends Model
'is_private',
];

protected array $sortable = [
'character.name',
'character.type',
];

public function character(): BelongsTo
{
return $this->belongsTo(Character::class);
Expand All @@ -46,4 +51,9 @@ public function getCharacterRacesAttribute()
{
return $this->character->races;
}

public function getCharacterLocationAttribute()
{
return $this->character->location;
}
}
14 changes: 13 additions & 1 deletion app/Models/Race.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public function characterRaces(): HasMany
}

/**
* Get all characters in the location and descendants
* Get all characters in the race and descendants
*/
public function allCharacters()
{
Expand All @@ -206,6 +206,18 @@ public function allCharacters()
->whereIn('cr.race_id', $raceIds);
}

/**
* Get all characters in the race and descendants
*/
public function allCharacterRaces()
{
$raceIds = [$this->id];
foreach ($this->descendants as $descendant) {
$raceIds[] = $descendant->id;
};
return CharacterRace::groupBy('character_id')->distinct('character_id')->whereIn('race_id', $raceIds)->with('character');
}

/**
* Get the entity_type id from the entity_types table
*/
Expand Down
18 changes: 13 additions & 5 deletions app/Renderers/Layouts/Race/Character.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,35 @@ public function columns(): array
'with' => ['target' => 'character']
],
'character_id' => [
'key' => 'name',
'key' => 'character.name',
'label' => Module::singular(config('entities.ids.character'), 'entities.character'),
'render' => Standard::CHARACTER,
],
'type' => [
'key' => 'type',
'key' => 'character.type',
'label' => 'crud.fields.type',
'render' => function ($model) {
return $model->character->type;
},
],
'location' => [
'key' => 'location.name',
'with' => 'character',
'label' => Module::singular(config('entities.ids.location'), 'entities.location'),
'render' => Standard::LOCATION,
],
'races' => [
'label' => Module::plural(config('entities.ids.race'), 'entities.races'),
'class' => self::ONLY_DESKTOP,
'render' => Standard::ENTITYLIST,
'with' => 'characterRaces',
'render' => function ($model) {
$races = [];
foreach ($model->character->characterRaces as $race) {
if (!$race->race || !$race->race->entity) {
continue;
}
$races[] = $this->entityLink($race->race);
}
return implode(', ', $races);
},
'visible' => function () {
return !request()->has('race_id');
}
Expand Down

0 comments on commit e71b474

Please sign in to comment.