【发布时间】:2017-05-07 00:29:34
【问题描述】:
我想听听您对此问题的提示或解决方案。更有帮助的是一些工作的链接!实现其中一些机制的开源项目。
例如,我想将所有 github 的要点与 Laravel 模型同步。我考虑了两种形式。
选项 1
编写单独的类,例如 Importer,并从控制器和命令调用它的方法并与 Model 交互。
选项 2
将数据提取逻辑(api 调用)放入 Laravel 的模型层次结构(特征、接口等)和工匠的命令中,让我们说 synchronize:all 循环遍历所有模型并检查任何更改并更新每一行。
示例
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Sample\Http\Client;
class Gist extends Model
{
/**
* Example of fetching new gists
*/
public static function getNew($gistId = '6cad326836d38bd3a7ae')
{
$resposne = Client::request('GET', 'https://api.github.com/gists/'.$gistId)
->response()
->json();
$Gist = new static($response);
$Gist->save();
return $Gist;
}
/**
* Get list of all gist from external source and ... who knows
*/
public function listAllGistFromApi()
{
$resposne = Client::request('GET', 'https://api.github.com/gists/')
->response()
->json();
return $response;
}
/**
* Example of updating existing gist
*/
public function updateFromApi()
{
$gistId = $this->id; // primary key also api uid
$resposne = Client::request('GET', 'https://api.github.com/gists/'.$gistId)
->response()
->json();
return $this->setAttributes($response)->save(); // simplified logic
}
}
您的解决方案...
【问题讨论】:
-
我不需要stackoverflow的代码。我想要的是你对这个问题的想法。
标签: php laravel api eloquent synchronization