laravel学习笔记--数据库篇

66 篇文章 4 订阅

执行原生 SQL

执行原生SQL语句还是非常少用的,一般都是用构造器

执行原生 SQL 查询

$users = DB::select('select * from users where id = ?', [1]);

使用命名绑定
除了使用 ? 表示参数绑定外,你还可以使用命名绑定的形式来执行一个查询

$users = DB::select('select * from users where id = :id', ['id' => 1]);

执行普通语句
有些数据库语句不会有任何返回值。对于这些语句,可以使用statement 方法来运行

DB::statement('drop table users');

查询构造器

从一个数据表中获取所有行

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

这种方式的返回值是数组对象,取得值可以这样取

$users[0]->name//[0]是数组中的第几条数组,数组里面的数据就是对象了

获取单条数据

$users = DB::table('users')->where('id', '=', 1)->first();

获取单个字段的值

$users = DB::table('users')->where('id', '=', 1)->value('email');

如果明知道id作为查询条件的话,可以直接使用find()来查询

$users = DB::table('users')->find(1);

获取单列数据

$users = DB::table('users')->pluck('email');

还可以在获取单列数据的同时去指定键名

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

聚合

查询构造器还提供了各种聚合方法,比如countmaxminavg,还有 sum。可以在构造查询后调用任何方法

$users = DB::table('users')->count();//构造器的count方法
$users = DB::table('users')->get()->count();//集合的count方法

判断记录是否存在–返回布尔值

$users = DB::table('users')->where('id', '=', 1)->exists();

判断记录不存在–返回布尔值

$users = DB::table('users')->where('id', '=', 1)->doesntExist();

有时候可能不是总是希望从数据库表中获取所有列。使用 select 方法,你可以自定义一个 select 查询语句来查询指定的字段

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

Join语句

查询构造器也可以编写 join 方法。若要执行基本的「内链接」,你可以在查询构造器实例上使用 join 方法。传递给 join
方法的第一个参数是你需要连接的表的名称,而其他参数则使用指定连接的字段约束。你还可以在单个查询中连接多个数据表

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

Or 语句(或者)

可以一起链式调用 where 约束,也可以在查询中添加 or 子句。 orWhere 方法和 where 方法接收的参数一样

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

还可以使用闭包的方式

如果需要在括号内对 or 条件进行分组,将闭包作为 orWhere 方法的第一个参数也是可以的

$users = DB::table('users')
            ->where('votes', '>', 100)
            ->orWhere(function($query) {
                $query->where('name', 'Abigail')//$query是已查询的数据
                      ->where('votes', '>', 50);
            })
            ->get();

latest / oldest(日期排序)

latest 和 oldest 方法让你以一种便捷的方式通过日期进行排序。它们默认使用 created_at
列作为排序依据。当然,你也可以传递自定义的列名

$user = DB::table('users')
                ->latest()
                ->first();

inRandomOrder(随机排序)

inRandomOrder 方法被用来将结果进行随机排序。例如,你可以使用此方法随机找到一个用户

$randomUser = DB::table('users')
                ->inRandomOrder()
                ->first();

groupBy / having(分组)

groupBy 和 having 方法用于将结果分组。 having 方法的使用与 where 方法十分相似

$users = DB::table('users')
                ->groupBy('account_id')
                ->having('account_id', '>', 100)
                ->get();

skip / take(限制数量)

要限制结果的返回数量,或跳过指定数量的结果,你可以使用 skip 和 take 方法

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

还可以使用 limit 和 offset 方法去限制数量

$users = DB::table('users')
                ->offset(10)
                ->limit(5)
                ->get();

插入数据库

插入单条数据

$users = DB::table('users')->insert([
            'email' => 'xiaoxin@qq.com',
            'password' => bcrypt('123456'),
            'name' => '小信'
        ]);

插入多条数据

$users = DB::table('users')->insert([
            [
            'email' => 'xiaoxin6@qq.com',
            'password' => bcrypt('123456'),
            'name' => '小信6'
            ],
            [
                'email' => 'xiaoxin1@qq.com',
                'password' => bcrypt('123456'),
                'name' => '大信'
            ],
            [
                'email' => 'xiaoxin2@qq.com',
                'password' => bcrypt('123456'),
                'name' => '小信2'
            ],
            [
                'email' => 'xiaoxin3@qq.com',
                'password' => bcrypt('123456'),
                'name' => '小信3'
            ],
            [
                'email' => 'xiaoxin4@qq.com',
                'password' => bcrypt('123456'),
                'name' => '小信4'
            ]
        ]);

获取自增id

如果数据表有自增 ID ,使用 insertGetId 方法来插入记录可以返回 ID 值

$id = DB::table('users')->insertGetId(
    ['email' => 'john@example.com', 'votes' => 0]
);

更新
更新时如果不存在匹配记录则创建它

updateOrInsert 方法将首先尝试使用第一个参数的键和值对来查找匹配的数据库记录。
如果记录存在,则使用第二个参数中的值去更新记录。 如果找不到记录,将插入一个新记录,新增的数据是两个数组的集合

DB::table('users')
    ->updateOrInsert(
        ['email' => 'john@example.com', 'name' => 'John'],
        ['votes' => '2']
    );

自增自减
increment自增 和 decrement自减

DB::table('users')->increment('count');

注意:当你使用 increment 和 decrement 方法的时候,不会触发模型的事件。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小信啊啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值