$value) { $this->setAttribute($key, $value); } parent::__construct($attributes); } /** * 获取系列信息 */ public function getSeriesAttribute($value) { if (is_array($value)) { return (object) $value; } return $value; } /** * 从 API 获取所有数据 */ public static function fromApi($page = 1, $perPage = 10) { $apiService = app(\App\Services\TextbookApiService::class); $params = [ 'page' => $page, 'per_page' => $perPage, ]; $result = $apiService->getTextbooks($params); // 返回 Eloquent 模型实例(但禁用数据库查询) $records = []; foreach ($result['data'] ?? [] as $item) { $records[] = new static($item); } return new Collection($records); } /** * 从 API 获取总数 */ public static function countFromApi() { $apiService = app(\App\Services\TextbookApiService::class); $params = ['page' => 1, 'per_page' => 1]; $result = $apiService->getTextbooks($params); return $result['meta']['total'] ?? 0; } /** * 覆盖查询构建器方法,防止数据库查询 */ public function newQuery() { throw new \Exception('External data source - use getTableRecords() instead'); } public static function query() { throw new \Exception('External data source - use getTableRecords() instead'); } }