我正在为API使用Laravel后端的iphone应用程序。在某些时候,将会有Goole Places集成(或类似的服务)。Laravel - 第三方API的模型?
在我的应用程序中,我需要存储用户和地点之间的关系,这是多对多关系。然而,因为“地点”不是由Eloquent模型(而是由Google的API)表示的,我该如何创建这种关系?我是否需要为Google Places API创建包装?
我正在为API使用Laravel后端的iphone应用程序。在某些时候,将会有Goole Places集成(或类似的服务)。Laravel - 第三方API的模型?
在我的应用程序中,我需要存储用户和地点之间的关系,这是多对多关系。然而,因为“地点”不是由Eloquent模型(而是由Google的API)表示的,我该如何创建这种关系?我是否需要为Google Places API创建包装?
这是我该怎么做的。
您所有的型号都从基地Eloquent ORM model class延伸,这意味着您可以在扩展课程中覆盖查找器/获取器/设置器等。
您的Google Place模型是一种特殊情况,它不依赖于数据库,而是依赖于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?
}
}
上。然后,你只需要用户在使用谷歌的地方ID数据透视表的地方联系起来。
我认为为第三方制作模型并不相关,因为它不是服务和使用API的雄辩角色。 否则我建议你做一个服务:
public function test(MyService $service)