所以我的流程是这样的
API 调用一个 json 文件,检索看起来像这样的数据:(让我们调用这个 API 调用)
[
{
"id": 5123
"title": "Title 1",
"sizeOnDisk": 2040422598,
"status": "released",
"website": "",
...
},
{
"id": 6123
"title": "Title 2",
"sizeOnDisk": 2040422598,
"status": "released",
"website": "",
...
},
{
"id": 7123
"title": "Title 3",
"sizeOnDisk": 2040422598,
"status": "released",
"website": "",
...
},
]
现在,在我获得这些数据后,我从一个数据库表中获取所有数据,它看起来像这样:
| id | title | api_id |
------------------------
| 10 | Title 1 | 5123 |
| 12 | Title 3 | 7123 |
现在,如果您注意到,我在数据库的 api 中缺少标题 2。我正在尝试从不在数据库中的 api 中返回丢失的项目。
截至目前,我已经弄清楚如何返回丢失项目的 ID,如下所示:
$api = new Api();
$remoteIds = collect(json_decode(file_get_contents($api->url('movie'))))->pluck('id');
$localIds = Movie::all()->pluck('radarr_id');
$diff = $remoteIds->diff($localIds)->flatten();
return $diff;
...但我需要返回整行。如果我删除->pluck('id')
,我会收到一个Object of class stdClass could not be converted to string
错误..
那么我怎么能从 api 返回丢失项目的全部数据呢?我知道我可以做,array_intersect_key
但关键是不同的..
您可以使用filter而不是diff。
$api = new Api();
$remoteIds = collect(json_decode(file_get_contents($api->url('movie'))));
$movies = Movie::all()->pluck('id')->values();
$diff = $remoteIds->filter(function($item) use ($movies) {
return in_array($item->id, $movies);
});
return $diff; // Get the list of objects, not only ids
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句