belongsTo(Seller::class, 'seller_id', 'id')->withDefault(); } public function storage() { return $this->belongsTo(Storage::class)->withDefault(); } public function category() { return $this->belongsTo(Category::class)->withDefault(); } protected function getStatusTextAttribute() { if ($this->status == 1) { if ($this->start_at->timestamp > time()) { return "还没开始"; } elseif ($this->end_at->timestamp < time()) { return "已经结束"; } return "正常"; } else { return "关闭"; } } public function logs() { return $this->HasMany(SellerLessonLog::class, 'lesson_id', 'id'); } public function getTitle() { return $this->title; } public function getPrice() { return $this->price; } public function getScore() { return 0; } public function getStock() { return $this->stock; } public function getSellerPrice() { return $this->price; } public function deductStock($stock) { $this->decrement('stock', $stock); } public function addStock($stock) { $this->increment('stock', $stock); } public function getStorage() { return $this->storage; } /** * 可报名 * @return boolean */ public function canCart(): bool { return Cart::where('user_id', Auth::id())->where('lesson_id', $this->id)->count(); } protected function getCanCartTextAttribute() { return $this->canCart(); } //是否已购买 public function getBuyTextAttribute() { return $this->logs()->where('lesson_id', $this->id)->where('status', 1)->where('user_id', Auth::id())->count() ?? 0; } //是否已购买 public function getButtonTextAttribute() { if ($this->buy_text) { return "学习中"; } elseif ($this->canCart()) { return "已报名"; } else { return "立即报名"; } } //是否已购买 public function getButtonValueAttribute() { if ($this->buy_text) { return 1; } elseif ($this->canCart()) { return 2; } else { return 0; } } }