$sprouts * @property string|null $attachment_image_path * @property Carbon|null $created_at * @property Carbon|null $updated_at * @property-read \Illuminate\Database\Eloquent\Collection $efforts * @property-read int|null $efforts_count * @property-read User $user * * @method static \Database\Factories\SeedFactory factory($count = null, $state = []) * @method static \Illuminate\Database\Eloquent\Builder|Seed newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|Seed newQuery() * @method static \Illuminate\Database\Eloquent\Builder|Seed query() * @method static \Illuminate\Database\Eloquent\Builder|Seed whereAttachmentImagePath($value) * @method static \Illuminate\Database\Eloquent\Builder|Seed whereCreatedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|Seed whereEssence($value) * @method static \Illuminate\Database\Eloquent\Builder|Seed whereId($value) * @method static \Illuminate\Database\Eloquent\Builder|Seed whereName($value) * @method static \Illuminate\Database\Eloquent\Builder|Seed whereSpecies($value) * @method static \Illuminate\Database\Eloquent\Builder|Seed whereSprouts($value) * @method static \Illuminate\Database\Eloquent\Builder|Seed whereUpdatedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|Seed whereUserId($value) * * @mixin \Eloquent */ class Seed extends Model { /** @use HasFactory */ use HasFactory; protected function casts(): array { return [ 'sprouts' => AsArrayObject::class, 'species' => SeedSpecies::class, ]; } protected $attributes = [ 'species' => SeedSpecies::WILD->value, ]; /** * @return BelongsTo */ public function user(): BelongsTo { return $this->belongsTo(User::class); } /** * @return HasMany */ public function efforts(): HasMany { return $this->hasMany(Effort::class); } public static function getSpeciesWithCounts(User $user): Collection { /** * This doesn't include species that don't * have any seeds associated with them. */ $speciesWithCounts = $user->seeds() ->selectRaw('species, count(*) as count') ->groupBy('species') ->pluck('count', 'species'); /** * Use a default value of 0 if $species * isn't present in $speciesWithCounts * And append an 'all' key with the total * number of seeds for $user to the collection. */ return collect(SeedSpecies::cases()) ->mapWithKeys(fn ($species) => [ $species->value => $speciesWithCounts->get($species->value, 0), ])->put('all', $user->seeds()->count()); } }