zenkilies / igdb-laravel
IGDB API v3 的 Laravel 封装
Requires
- php: ^7.2.5
- ext-json: *
- guzzlehttp/guzzle: ~6.0
- laravel/framework: 5.8.*|^6.0|^7.0
README
这是一个用于 IGDB API(Apicalypse)版本 3 的 Laravel 封装。
基本安装
您可以使用 composer 安装此包:
composer require marcreichel/igdb-laravel
该包将自动注册其服务提供者。
要发布配置文件到 config/igdb.php,请运行:
php artisan vendor:publish --provider="MarcReichel\IGDBLaravel\IGDBLaravelServiceProvider"
这是配置文件的默认内容:
return [ /* * This is the API Token you got from https://api.igdb.com */ 'api_token' => env('IGDB_TOKEN', ''), /* * This package caches queries automatically (for 1 hour per default). * Here you can set how long each query should be cached (in seconds). * * To turn cache off set this value to 0 */ 'cache_lifetime' => env('IGDB_CACHE_LIFETIME', 3600), /* * This is the per-page limit for your tier. */ 'per_page_limit' => 500, /* * This is the offset limit for your tier. */ 'offset_limit' => 5000, ];
用法
如果您熟悉 Laravel 的 Eloquent 系统 和 查询构建器,您将喜欢此包,因为它采用了类似的方法。
模型
API 的每个端点都映射到其自己的模型。
要获取游戏列表,您可以简单地调用类似以下内容:
use MarcReichel\IGDBLaravel\Models\Game; $games = Game::where('first_release_date', '>=', 1546297200)->get();
以下是所有可用的模型列表:
- 成就
- 成就图标
- 年龄评级
- 年龄评级内容描述
- 别名
- 艺术品
- 角色
- 角色头像
- 收藏
- 公司
- 公司标志
- 公司网站
- 封面
- 外部游戏
- 动态
- 系列
- 游戏
- 游戏引擎
- 游戏引擎标志
- 游戏模式
- 游戏版本
- 游戏版本特性
- 游戏版本特性值
- 游戏视频
- 类型
- 参与公司
- 关键词
- 多人模式
- 页面
- 页面背景
- 页面标志
- 页面网站
- 平台
- 平台标志
- 平台版本
- 平台版本公司
- 平台版本发布日期
- 平台网站
- 玩家视角
- 产品系列
- 脉冲
- 脉冲组
- 脉冲源
- 脉冲网址
- 发布日期
- 截图
- 搜索
- 主题
- 击败时间
- 标题
- 网站
查询构建器
您可以使用上面列出的定义好的模型之一。搜索结果将自动映射到使用的模型。以下示例中使用了这种方法。
否则,您可以像这样使用查询构建器本身:
use MarcReichel\IGDBLaravel\Builder as IGDB; $igdb = new IGDB('games'); // 'games' is the endpoint $games = $igdb->get();
选择
选择响应中应包含的字段。如果您想包含所有可用字段,也可以跳过此方法,因为查询构建器默认将选择 *。(注意:这与 Apicalypse API 的行为相反)
$games = Game::select(['*'])->get(); $games = Game::select(['name', 'first_release_date'])->get();
搜索
$games = Game::search('Fortnite')->get();
Where 子句
简单的 Where 子句
$games = Game::where('first_release_date', '>=', 1546297200)->get();
为了方便,如果您想验证列是否等于给定的值,可以将值直接作为 where 方法的第二个参数传递
$games = Game::where('name', 'Fortnite')->get();
Or 语句
您可以将 where 约束链在一起,也可以向查询中添加 or 子句。 orWhere 方法接受与 where 方法相同的参数
$games = Game::where('name', 'Fortnite')->orWhere('name', 'Borderlands 2')->get();
额外的 Where 子句
whereBetween
whereBetween 方法验证字段值是否在两个值之间
$games = Game::whereBetween('first_release_date', 1546297200, 1577833199)->get();
whereNotBetween
whereNotBetween 方法验证字段值是否不在两个值之间
$games = Game::whereNotBetween('first_release_date', 1546297200, 1577833199)->get();
whereIn
whereIn 方法验证给定字段值是否包含在给定的数组中
$games = Game::whereIn('category', [0,4])->get();
whereNotIn
whereNotIn 方法验证给定字段值是否不包含在给定的数组中
$games = Game::whereNotIn('category', [0,4])->get();
whereInAll / whereNotInAll / whereInExact / whereNotInExact
或者您可以使用这些方法之一来匹配给定的 所有 或 精确 数组。
whereNull
《whereNull》方法用于验证指定字段的值为NULL
$games = Game::whereNull('first_release_date')->get();
whereNotNull
《whereNotNull》方法用于验证字段的值不是NULL
$games = Game::whereNotNull('first_release_date')->get();
whereDate
《whereDate》方法可以用来比较字段的值与日期
$games = Game::whereDate('first_release_date', '2019-01-01')->get();
whereYear
《whereYear》方法可以用来比较字段的值与特定年份
$games = Game::whereYear('first_release_date', 2019)->get();
whereHas / whereHasNot
这些方法与《whereNull》和《whereNotNull》具有相同的语法,实际上做的是完全相同的事情。
参数分组
$games = Game::where('name', 'Fortnite') ->orWhere(function($query) { $query->where('aggregated_rating', '>=', 90) ->where('aggregated_rating_count', '>=', 3000); })->get();
排序、限制和偏移
orderBy
《orderBy》方法允许您通过指定的字段对查询结果进行排序。向《orderBy》方法传递的第一个参数是您希望排序的字段,第二个参数控制排序方向,可以是asc(升序)或desc(降序)
$games = Game::orderBy('first_release_date', 'asc')->get();
skip / take
要限制查询返回的结果数量,或者要跳过查询中的给定数量的结果,可以使用《skip》和《take》方法(这两种方法都受限于您的当前层级,请确保在配置文件中正确配置它们)
$games = Game::skip(10)->take(5)->get();
或者,您也可以使用《limit》和《offset》方法
$games = Game::offset(10)->limit(5)->get();
缓存
您可以覆盖特定查询的默认缓存时间。例如,您可以为查询关闭缓存
$games = Game::cache(0)->get();
获取
要最终获取查询结果,只需调用《get》即可
$games = Game::get();
所有
如果您只想获取“所有”结果(受限于您层级的每页限制),只需直接在您的模型上调用《all》方法
$games = Game::all();
第一个
如果您只想获取一个结果,请在查询后调用《first》方法
$game = Game::first();
查找
如果您知道模型的标识符,可以直接使用标识符作为参数调用《find》方法
$game = Game::find(1905);
FindOrFail
《find》在没有找到结果时返回null。如果您想抛出一个异常,请使用《findOrFail》。如果没有找到结果,这将抛出MarcReichel\IGDBLaravel\Exceptions\ModelNotFoundException异常。
关系(扩展)
要扩展结果,请使用《with》方法
$game = Game::with(['cover', 'artworks'])->get();
默认情况下,关系中的每个字段(*)都被选中。如果您想自己定义关系的字段,您必须将关系定义为数组键,字段定义为数组
$game = Game::with(['cover' => ['url', 'image_id'])->get();
读取属性
基于模型的方法
如果您使用了基于模型的方法,您可以直接获取属性
$game = Game::find(1905); if ($game) { echo $game->name; // Will output "Fortnite" }
如果您想访问不存在的属性,将返回null
$game = Game::find(1905); if ($game) { echo $game->foo; // Will output nothing }
基于查询构建器的方法
如果您使用了查询构建器本身,您必须自己检查属性是否存在。
待办事项列表
- 重构代码(美化代码)
- 编写单元测试
贡献
欢迎提交拉取请求 :)