laravel mysql

一、常用查询语句

1、从数据表中取得单一数据列

$user= DB::table('users')->where('name','John')->first();

2、检索表中的所有行

$users = DB::table('users')->get();
foreach ($users as $user) {
var_dump($user->name);
}

3、从表检索单个行

$user = DB::table('users')->where('name', 'John')->first();
var_dump($user->name);

检索单个列的行

$name = DB::table('users')->where('name', 'John')->pluck('name');

检索一个列值列表

$roles = DB::table('roles')->lists('title');

该方法将返回一个数组标题的作用。你也可以指定一个自定义的键列返回的数组

$roles = DB::table('roles')->lists('title', 'name');

指定一个Select子句

$users = DB::table('users')->select('name', 'email')->get();
$users = DB::table('users')->distinct()->get();
$users = DB::table('users')->select('name as user_name')->get();

Select子句添加到一个现有的查询$query = DB::table('users')->select('name');

$users = $query->addSelect('age')->get();

where

$users = DB::table('users')->where('votes', '>', 100)->get();

OR

$users = DB::table('users')->where('votes', '>', 100)->orWhere('name', 'John')->get();

Where Between

$users = DB::table('users')->whereBetween('votes', array(1, 100))->get();

Where Not Between
 

$users = DB::table('users')->whereNotBetween('votes', array(1, 100))->get();

Where In With An Array

$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();
$users = DB::table('users')->whereNotIn('id', array(1, 2, 3))->get();

Using Where Null To Find Records With Unset Values

$users = DB::table('users')->whereNull('updated_at')->get();

Order By, Group By, And Having

$users = DB::table('users')->orderBy('name', 'desc')->groupBy('count')->having('count', '>', 100)->get();

Offset & Limit

$users = DB::table('users')->skip(10)->take(5)->get();

二、连接

Joins

查询构建器也可以用来编写连接语句。看看下面的例子:

Basic Join Statement

DB::table('users')
  ->join('contacts', 'users.id', '=', 'contacts.user_id')
  ->join('orders', 'users.id', '=', 'orders.user_id')
  ->select('users.id', 'contacts.phone', 'orders.price')
  ->get();

左连接语句

DB::table('users')
  ->leftJoin('posts', 'users.id', '=', 'posts.user_id')
  ->get();

DB::table('users')
  ->join('contacts', function($join)
  {
  $join->on('users.id', '=', 'contacts.user_id')->orOn(...);
  })
  ->get();

DB::table('users')
  ->join('contacts', function($join)
  {
  $join->on('users.id', '=', 'contacts.user_id')
  ->where('contacts.user_id', '>', 5);
  })
  ->get();

三、分组

有时候,您可能需要创建更高级的where子句,如“存在”或嵌套参数分组。Laravel query builder可以处理这些:

DB::table('users')
->where('name', '=', 'John')
->orWhere(function($query)
{
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();

上面的查询将产生以下SQL:
 

select * from users where name = 'John' or (votes > 100 and title 
<> 'Admin')
  Exists Statements
  DB::table('users')
  ->whereExists(function($query)
  {
  $query->select(DB::raw(1))
  ->from('orders')
  ->whereRaw('orders.user_id = users.id');
  })
  ->get();

四、事务

laravel transaction : laravel 的事务是不支持eloquent的, 要用DB::的方式

数据库事务处理#

你可以使用 transaction 方法,去执行一组数据库事务处理的操作:

DB::transaction(function()
{
    DB::table('users')->update(['votes' => 1]);

    DB::table('posts')->delete();
});

注意: 在 transaction 闭包若抛出任何异常会导致事务自动回滚。

有时候你可能需要自己开始一个事务:

DB::beginTransaction();

你可以通过 rollback 的方法回滚事务:

DB::rollback();

最后,你可以通过 commit 的方法提交事务:

DB::commit();

五、获取连接#

若要使用多个连接,可以通过 DB::connection 方法取用:

$users = DB::connection('foo')->select(...);

你也可以取用原始底层的 PDO 实例:

$pdo = DB::connection()->getPdo();

有时候你可能需要重新连接到特定的数据库:

DB::reconnect('foo');

如果你因为超过了底层 PDO 实例的 max_connections 的限制,需要关闭特定的数据库连接,可以通过 disconnect 方法:

DB::disconnect('foo');

六、查找日志记录#

Laravel 可以在内存里访问这次请求中所有的查找语句。然而在有些例子下要注意,比如一次添加 大量的数据,可能会导致应用程序耗损过多内存。 如果要启用日志,可以使用 enableQueryLog 方法:

DB::connection()->enableQueryLog();

要得到执行过的查找纪录数组,你可以使用 getQueryLog 方法:

$queries = DB::getQueryLog();

猜你喜欢

转载自blog.csdn.net/u012160319/article/details/82052270