app/Models/Card.php: in getBySettings() move settings stuff to its own function settingsQuery()
This commit is contained in:
parent
7b513f5040
commit
5ea912137b
1 changed files with 17 additions and 9 deletions
|
|
@ -9,6 +9,7 @@ use Kirschbaum\PowerJoins\PowerJoins;
|
|||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
|
|
@ -79,6 +80,21 @@ class Card extends Model
|
|||
*/
|
||||
public static function getBySettings(GameSettings $settings, ?int $max = null)
|
||||
{
|
||||
$query = self::settingsQuery($settings)
|
||||
->inRandomOrder();
|
||||
|
||||
if ($max !== null) {
|
||||
$query->limit($max);
|
||||
}
|
||||
|
||||
return $query->get()
|
||||
->makeHidden(['character', 'class', 'deleted_at']);
|
||||
}
|
||||
|
||||
protected static function settingsQuery(GameSettings $settings): Builder
|
||||
{
|
||||
$query = self::query();
|
||||
|
||||
// Map type of setting to database column.
|
||||
$type_map = [
|
||||
'characters' => 'character_id',
|
||||
|
|
@ -87,9 +103,6 @@ class Card extends Model
|
|||
'roles' => 'role'
|
||||
];
|
||||
|
||||
$query = self::query()
|
||||
->inRandomOrder();
|
||||
|
||||
// Run through all settings and apply filter.
|
||||
foreach ($settings->all() as $type => $values) {
|
||||
|
||||
|
|
@ -100,11 +113,6 @@ class Card extends Model
|
|||
});
|
||||
}
|
||||
|
||||
if ($max !== null) {
|
||||
$query->limit($max);
|
||||
}
|
||||
|
||||
return $query->get()
|
||||
->makeHidden(['character', 'class', 'deleted_at']);
|
||||
return $query;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue