• 数据库操作
use Illuminate\Support\Facades\DB;
DB::select("select * from users where name=?",['admin']);  //也支持name=:name,[':name'=>'admin']绑定,返回obj
DB::insert('insert into users (id, name) values (?, ?)', [1, 'Dayle']);
$affected = DB::update('update users set votes = 100 where name = ?', ['John']); 返回影响行数
$deleted = DB::delete('delete from users');返回影响行数
DB::statement('drop table users'); 其他无返回语句
DB::transaction(function () { 事务
    DB::table('users')->update(['votes' => 1]);

    DB::table('posts')->delete();
});
$list = DB::table("users")->get()->toArray();//获取全部转换成数组
DB::table("users")->first() 获取一行
DB::table("users")->value('email'); 获取一个字段
DB::table("users")->pluck('email')->toArray(); 获取一列,如果设置两个pluck('name','id') id是键name是值的键值数组
count() 结果集数量  max('price')最大值 min('price')最小值 avg('price')平均数 sum('price')求和
DB::table('users')->select('name', 'email as user_email')->get(); 返回特定字段
whereRaw('price > IF(state = "TX", ?, 100)', [200]) 原生where
orderBy('name', 'desc')  inRandomOrder()随机排序
join('contacts', 'users.id', '=', 'contacts.user_id') 内连接
leftJoin('posts', 'users.id', '=', 'posts.user_id') 左连接
groupBy('account_id') 分组 groupBy('first_name', 'status')
having('account_id', '>', 100) 刷选
take(5) 返回5条
->offset(10) ->limit(5) 限制
insert( ['email' => 'john@example.com', 'votes' => 0]); 插入 支持多条数据
$id=DB::table("users")-> insertGetId( ['email' => 'john@example.com', 'votes' => 0]); 插入后返回id
update(['votes' => 1]); 更新
increment('votes', 5);decrement('votes', 5);自增自减
increment('votes', 1, ['name' => 'John']); 支持其他字段更新
DB::table('users')->truncate(); 清空表
DB::table('users')->where('votes', '>', 100)->delete(); 删除 
$users = DB::table('users')->paginate(15);  分页
$users = DB::table('users')->simplePaginate(15); 简单分页只有上一页下一页
$results->total() 总页数
<div class="container">
    @foreach ($users as $user)
        {{ $user->name }}
    @endforeach
</div>
{{ $users->links() }}
  • redis
use Illuminate\Support\Facades\Redis;
Redis::get('id');
Redis::set('name', 'Taylor');
Redis::command('set', ['name',34]); 支持所有原生

单独使用 composer require illuminate/database

include __DIR__ . '/vendor/autoload.php';
$database = [
    'driver' => 'mysql',
    'host' => "localhost",
    'database' => "test",
    'username' => "root",
    'password' => "mysql",
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => "",
];
use Illuminate\Container\Container;
use Illuminate\Database\Capsule\Manager as DB;
$db = new DB;
$db->addConnection($database);
$db->setAsGlobal();
$db->bootEloquent();
//创建表语句
/*DB::schema()->create('user', function($table)
{
    $table->increments('id');
    $table->string('username', 40);
    $table->string('email')->unique();
    $table->timestamps();
});*/
//插入演示数据
/*DB::table('user')->insert(array(
    array('username' => 'Helloworld',  'email' => '852369@qq.com'),
    array('username' => 'shennanluban',  'email' => 'shennanluban@qq.com'),
    array('username' => 'xiaoxuesheng',  'email' => 'xiaoxuesheng@qq.me'),
));*/
$users = DB::select('select * from user where 1=1');
dump($users);
$list = DB::table("user")->get()->toArray();
dump($list);
文档更新时间: 2020-10-24 18:32   作者:Yoby