Laravel - 第三方 API 的模型?

标签 laravel orm eloquent many-to-many

我正在开发一个 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/

相关文章:

php - Laravel 用户特定的缓存

php - 如何用join来统计mysql?

php - 使用 php (laravel) 对对象进行排序

php - 找不到类 'Maatwebsite\Excel\Excel'

php - Laravel 5 Auth - 用户自定义表

javascript - Vue.js : Best way to implement MPA(Multi page app) in laravel

php - 在 Laravel 中加入 select

java - 没有运算符匹配给定的名称和参数类型。您可能需要添加显式类型转换。 -- Netbeans、Postgresql 8.4 和 Glassfish

Django .get() 在没有显式 '__exact' 查找的情况下引发了DoesNotExist 错误

java - Hibernate ORM 建模复合键何时将数据插入 MYSQL