我正在开发一个 iPhone 应用程序,它使用 Laravel 后端作为 API。在某个时候,将会有Google Places集成(或类似的服务)。
在我的应用程序中,我需要存储用户和地点之间的多对多关系。然而,由于“地点”不是由 Eloquent 模型(而是由 Google 的 API)表示,我如何创建这种关系?我需要为 Google Places API 创建包装器吗?
最佳答案
这是我的做法。
您的所有模型均从底座延伸 Eloquent ORM model class这意味着可以覆盖扩展类中的 finders/getters/setters 等。
您的 Google Places 模型是一种特殊情况,它不依赖于数据库,而是依赖于 Google Places API。
因此,您需要覆盖 Google Places 模型的基本 Eloquent 模型方法。
例如:
class GooglePlace extends Eloquent{
/**
* Find a model by its primary key or return new static.
*
* @param mixed $id
* @param array $columns
* @return \Illuminate\Support\Collection|static
*/
public static function findOrNew($id, $columns = ['*'])
{
# we do not need Eloquent's database based implementation
# api code to get Google Place using $id
# perhaps use $columns to get only specific fields about the place
# from the API?
}
}
然后,您只需使用 Google 地点 ID 将用户与数据透视表中的地点相关联即可。
关于Laravel - 第三方 API 的模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31894138/