[转自简书,讲PhalApi如何整合Laravel的ORM] Phalapi use laravel's eloquent orm

Phalapi use laravel's eloquent orm
 
Booter

a mini booter of some services .

Installation

Use composer to manage your dependencies and download:composer require fizzday/booterExample

1.Eloquent

config & boot
 
<?php
use Fizzday\Booter\Booter;
$config = array(
'default' => 'mysql',

'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => '192.168.200.248',
'port' => '3306',
'database' => 'wcc_service_yxpt',
'username' => 'gcore',
'password' => 'gcore',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
);
Booter::bootEloquent($config);
usage:
 
<?php

use Illuminate\Database\Eloquent\Model as Model;
use Illuminate\Database\Capsule\Manager as DB;

class UserModel extends Model {
protected $table = "prefix_users";
}

// eloquent 调用
UserModel::first();

// query builder 调用
DB::table("users")->First();
phalapi 中调用

在~/phalapi/config/di.php中添加代码:
 
<?php
......
use Fizzday\Booter\Booter;
$config = array(
'default' => 'mysql',

'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => '192.168.200.248',
'port' => '3306',
'database' => 'wcc_service_yxpt',
'username' => 'gcore',
'password' => 'gcore',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
);
$di->db = Booter::bootEloquent($config);
......
当然, 这里可以将配置文件抽取放到单独文件中
 
原文请访问:https://www.jianshu.com/p/198f44450ac1

0 个评论

要回复文章请先登录注册