netbuild/apidriver

一个支持 Restful Api 服务器使用的 Eloquent 模型和查询构建器,基于原始 Laravel API。该库扩展了原始 Laravel 类,因此使用完全相同的方法。支持使用 hasMany、belongsTo 等方法与其他模型建立关系。

v1.0.3 2021-01-28 15:14 UTC

This package is auto-updated.

Last update: 2024-09-21 03:55:32 UTC


README

一个支持 Restful Api 服务器使用的 Eloquent 模型和查询构建器,基于原始 Laravel API。该库扩展了原始 Laravel 类,因此使用完全相同的方法。支持使用 hasMany、belongsTo 等方法与其他模型建立关系。

与其他 Laravel 实例配合使用效果极佳。

安装

使用 composer 安装

composer require netbuild/apidriver

并在 config/app.php 中添加服务提供者

Netbuild\Apidriver\ApiDbServiceProvider::class

配置

config/database.php 中更改默认数据库连接名称

'default' => 'api'

并添加新的 api 服务器连接

'api' => [
        'driver' => 'api',
]

使用方法

创建新的模型,扩展 Api Eloquent 模型

use Netbuild\Apidriver\Eloquent\Model;

class User extends Model
{
	protected $url 			= 'https://api.your_restful.url';
	protected $api_token		= 'YOUR_API_TOKEN';
	protected $table 		= 'REMOTE_MODEL';
}

使用原始 Eloquent API

$users = User::where('id', '<', 100)->take(3)->get();

或者

$users = User::where('column_1', '=', 'your_term_1')->orWhere('column_1', '=', 'your_term_2')->take(3)->get();

或者

$user = User::find(3);

或者

$user->delete();

关系

用户模型

<?php

namespace App\Models\API;

use Netbuild\Apidriver\Eloquent\Model;

class User extends Model
{
    protected $connection   		= 'api';
    protected $url 			= 'https://api.your_restful.url';
    protected $api_token		= 'YOUR_API_TOKEN';
    protected $table 			= 'REMOTE_MODEL';
    public    $timestamps   		= true;

}

数据库模型

<?php

namespace App\Models\API;

use Netbuild\Apidriver\Eloquent\Model;

class Database extends Model
{
    protected $connection   		= 'api';
    protected $url 			= 'https://api.your_restful.url';
    protected $api_token		= 'YOUR_API_TOKEN';
    protected $table 			= 'REMOTE_MODEL';
    public    $timestamps   		= true;

    public function user()
    {
        return $this->belongsTo(User::class);
    }

}