laravel数据库操作

原生方法

Db::select('select * from users where active = ?',[1]);//查询,第一个参数原生语句第二个参数where条件
Db::insert('insert into users (id, name) values (?, ?)')//插入参数
Db::update('update users set votes = 100 where name = ?')//更新语句
Db::delete('delete form users where id = ?',[1]);//删除,一定要带条件否则会删除所有记录

 

查询构造器

Db::table('user')->get();//以对象形式返回user表的所有数据结果
Db::table('user')->where('id',1)->first();//只获取一行数据
Db::table('user')->where('id',1)->value('name');//获取单个字段
Db::table('user')->pluck('name');//获取一列
Db::table('user')->count();//获取总数
Db::table('user')->max('id');//获取id最大的数据
Db::table('user')->min('id');//获取id最小的数据
Db::table('user')->sum('');//查询和
Db::table('user')->distinct()->get();//返回不重复的结果集
Db::table('user')->select('name','email as user_email')->get();//获取指定列并定义email别名
$query = DB::table('users')->select('name');
$users = $query->addSelect('age')->get();//子查询
DB::table('users')
    ->join('contacts', 'users.id', '=', 'contacts.user_id')
    ->join('orders', 'users.id', '=', 'orders.user_id')
    ->select('users.*', 'contacts.phone', 'orders.price')
    ->get();//join查询内连接,leftjoin表示左连接,crossJoin交叉连接
Db::table('user')->insert();//插入
Db::table('user')->insertGetId();//插入并返回自增id
Db::table('user')->where('id',1)->update();//更新
Db::table('user')->increment('votes',5,['name'=>'张三']);//增加votes字段值,第二个参数不给默认为1,第三个参数是条件
Db::table('user')->decrement('votes',5,['name'=>'张三']);//减少votes字段值
Db::table('user')->where('name','张三')->delete();//删除
Db::table('user')->truncate();//截断表

 

where用法

where('id',1);//等于相当于where('id','=',1)
where('id','>=',100);//大于等于
where('id','<>',100);//不等于
where('id','like','T%');//模糊查询
where([['id',100],['status','=',1]]);//数组作为条件
where('id','>',10)->orWhere('name','张三');//or语句,多条件查询
whereBetween('id',[1,100]);//区间查询,查询1到100之间的数据
whereNotBetween('id',[1,100]);//区间查询,查询不在1到100之间的数据
whereIn('id',[1,2,3]);//数组查询,查询数组中给定条件对应的数据,此处查询id等于1,2,3的这3条数据
whereNotIn('id',[1,2,3]);//数组查询,查询查询数组中给顶条件之外的数据,此处查询id不等于1,2,3的所有数据
whereNull('name');//查询给定字段为null的数据
whereNotNull('name');//查询给定字段不为null的数据
whereDate('created_at','2019-08-01');//时间查询,查询某一天的数据
whereMonth('created_at','10');//时间查询,根据月份查询
whereDay('created_at','10');//时间查询,查询指定日期的数据
whereYear('created_at','2019');//时间查询,查询指定年份的数据
whereColumn('first_name','last_name');//查询两个字段相等的数据,还可用'>','<'查询

 

排序

Db::table('user')->orderBy('created_at','desc')->get();//倒叙查询
Db::table('user')->latest()->get();//通过日期对结果进行排序,默认以created_at进行排序
Db::table('user)->inRandomOrder()->first();//随机排序

 

分组

DB::table('user')->groupBy('account_id')->having('account_id', '>', 100)->get();//分组查询 having是条件

 

数据库事务

/*自动方式*/
DB::transaction(function () {
    //对表的操作
});//此方法遇到错误异常自动回滚,如果执行成功自动提交

/*手动方式*/
DB::beginTransaction();//开启事务
DB::rollBack();//事务回滚
DB::rollBack();//事务提交

 

读写分离

在database.php中配置
'mysql' => [
    'read' => [
        'host' => '127.0.0.1',
    ],
    'write' => [
        'host' => '127.0.0.2'
    ],
    'driver'    => 'mysql',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
],
//read为读的数据库,write为写的数据库,两个数据库采用同样的配置(用户名/密码等)
//单独配置方式如下
'rade' => [
  'host' => '127.0.0.1',
  'database' => 'name1',
  'username' => 'username1',
  'password' => 'password1',
],
'write' => [
  'host' => '127.0.0.2',
  'database' => 'name2',
  'username' => 'username2',
  'password' => 'password2',
]
//还可以配置多个读数据库随机选择
'rade' => [
  'host' => '127.0.0.1,127.0.0.3',
]

 

posted @ 2019-08-06 12:07  他的她  阅读(829)  评论(0编辑  收藏  举报