|MallItem newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|MallItem newQuery() * @method static \Illuminate\Database\Eloquent\Builder|MallItem query() * @method static \Illuminate\Database\Eloquent\Builder|MallItem whereCategoryId($value) * @method static \Illuminate\Database\Eloquent\Builder|MallItem whereDescription($value) * @method static \Illuminate\Database\Eloquent\Builder|MallItem whereId($value) * @method static \Illuminate\Database\Eloquent\Builder|MallItem whereImage($value) * @method static \Illuminate\Database\Eloquent\Builder|MallItem whereOldPrice($value) * @method static \Illuminate\Database\Eloquent\Builder|MallItem whereOther($value) * @method static \Illuminate\Database\Eloquent\Builder|MallItem wherePrice($value) * @method static \Illuminate\Database\Eloquent\Builder|MallItem wherePricing($value) * @method static \Illuminate\Database\Eloquent\Builder|MallItem whereQuantity($value) * @method static \Illuminate\Database\Eloquent\Builder|MallItem whereVnum($value) * @mixin \Eloquent */ class MallItem extends Model { /** * Indicates if the model should be timestamped. * * @var bool */ public $timestamps = false; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'id', 'category_id', 'old_price', 'price', 'pricing', 'quantity', 'image', 'description', 'other', ]; /** * The attributes that should be hidden for serialization. * * @var array */ protected $hidden = [ ]; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'pricing' => MallItemPricingEnum::class, ]; /** * Get the associated item_proto entry */ public function proto(): HasOne { return $this->hasOne(ItemProto::class, 'vnum', 'vnum'); } public function userCanBuy(): bool { $user = Auth::user(); if ($this->pricing == MallItemPricingEnum::CASH) { return $user->cash >= $this->price; } elseif ($this->pricing == MallItemPricingEnum::MILEAGE) { return $user->mileage >= $this->price; } return false; } public static function getSuggestions(int $maxCount, ?MallItem $forItem = null, ?string $frontpageDisplay = null) { $query = MallItem::query(); // Ignore the current item if specified if ($forItem) { $query = $query->whereNotIn('vnum', [$forItem->vnum]); } // Select items that are to be shown on the frontpage if ($frontpageDisplay) { $query = $query->where('other', $frontpageDisplay); } $items = $query->get(); // Just return what we selected if we don't have enough items in the database if ($items->count() <= $maxCount) { return $items; } return $items->random($maxCount); } }