php的db类库Eloquent单独使用系列(1)

[size=x-large]我的Eloquent单独使用系列文章[/size]
[url=http://xieye.iteye.com/blog/2382907]php的db类库Eloquent单独使用系列(1)[/url]
[url=http://xieye.iteye.com/blog/2383390]php的db类库Eloquent单独使用系列(2) - 分页[/url]
[url=http://xieye.iteye.com/blog/2387809]php的db类库Eloquent单独使用系列(3) - sql日志[/url]
[url=http://xieye.iteye.com/blog/2387983]php的db类库Eloquent单独使用系列(4)- 事件监听[/url]
[url=http://xieye.iteye.com/blog/2383466]php的db类库Eloquent单独使用系列(5)- 模型转数组[/url]
[url=http://xieye.iteye.com/blog/2388023]php的db类库Eloquent单独使用系列(6)- 一对一关联[/url]
[url=http://xieye.iteye.com/blog/2388029]php的db类库Eloquent单独使用系列(7)- 一对多关联[/url]
[url=http://xieye.iteye.com/blog/2388150]php的db类库Eloquent单独使用系列(8)- 多对多关联[/url]
[url=http://xieye.iteye.com/blog/2388162]php的db类库Eloquent单独使用系列(9)- 多对多关联 - 表关联自身[/url]
[url=http://xieye.iteye.com/blog/2388280]php的db类库Eloquent单独使用系列(10)- 多对多关联 - 远程一对多[/url]
[url=http://xieye.iteye.com/blog/2388521]php的db类库Eloquent单独使用系列(11)- 多对多关联 - 添加模型属性[/url]
[url=http://xieye.iteye.com/blog/2389182]php的db类库Eloquent单独使用系列(12)- 结果集模型转数组 - 2[/url]

elquent查询集锦:这是一个外链,作者chajinglong
[url=http://blog.csdn.net/chajinglong/article/details/51954010]Laravel SQL 查询语句集锦[/url]

composer:

"illuminate/database":"5.4.27",
"illuminate/events":"5.4.27" 这个可不加载,但使用它提供的模型类就要加载。

请注意,这个版本需要php5.6支持。

表结构如下:

CREATE TABLE `test_databases` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`db_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '库名',
`user_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '测试用户id',
`created_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
`updated_at` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '修改时间',
PRIMARY KEY (`id`),
UNIQUE KEY `test_databases_db_name_user_id_unique` (`db_name`,`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci



insert into test_databases values(1,'哈哈',1,11,22);
insert into test_databases values(2,'嗯嗯',2,33,44);


示例:

<?php

namespace app\control;
use \Illuminate\Database\Capsule\Manager as Capsule;
// use \Illuminate\Events\Dispatcher;
// use \Illuminate\Container\Container;

class Ill {

public function index( $req, $res, $args) {
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'test',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
$capsule->setAsGlobal();
$conn =$capsule;
echo "<h1>select(获取全部), (获取行)没有, fetchColumn没有</h1>";
$users = $conn::select('SELECT * FROM test_databases limit 2');
var_dump($users);
echo "<hr>";
//
echo "插入示例<br>";
$conn::insert('insert into test_databases (user_id, db_name) values (?, ?)', [time(), '汽车']);
// echo "插入id为:". $conn::insertGetId();
echo "修改示例<br>";
$result = $conn::update('update test_databases set user_id=user_id+'.mt_rand(10000,99999).
' where db_name=?', ['汽车']);
echo "更新影响的行数:". $result."<br>";
$result = $conn::delete('delete from test_databases where db_name=? limit 1', ['汽车']);
echo "删除影响的行数:". $result."<br>";

// 这里执行表结构定义,加减字段等语句
// DB::statement('drop table test_databases');
echo "<h2>查询构造器使用</h2>";
//获取行
echo "<h5>获取单行</h5>";
$user = $conn::table('test_databases')->where('id', 1)->first();

echo $user->db_name."<br>";
echo "<h5>获取单个值</h5>";
echo $conn::table('test_databases')->where('id', 1)->value('db_name') . "<br>";
echo "<h5>获取多行</h5>";
$user = $conn::table('test_databases')->where('id','<', 3)->get();
var_dump($user);echo "<br>";
echo $user[0]->db_name."<br>";

echo "<h5>获取一列数据</h5>";
$titles = $conn::table('test_databases')->where('id','<', 3)->pluck("db_name");
echo $titles[0];

echo "<h5>插入数据</h5>";
$id = $conn::table('test_databases')->insertGetId(
['db_name' => mt_rand(1000,10000), 'user_id' => mt_rand(1000,10000)]
);
echo "<h5>插入id:{$id}</h5>";

echo "<h5>更新数据示例</h5>";
$result = $conn::table('test_databases')
->where('id', 1)
->update(['user_id' => mt_rand(10,10000)]);
echo "更新行数". $result;


echo "<h5>删除数据示例</h5>";
$result = $conn::table('test_databases')
->where('id', $id)
->delete();
echo "删除行数". $result;
return $res;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值