前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Laravel 数据库交互 - 原生 SQL

Laravel 数据库交互 - 原生 SQL

作者头像
崔哥
发布2023-01-03 08:40:14
6050
发布2023-01-03 08:40:14
举报
文章被收录于专栏:崔哥的专栏
代码语言:javascript
复制
        return DB::connection('mysql')->select('SELECT * FROM `article` WHERE `category_id` = ?', [1]);
        return DB::select('SELECT * FROM `article` WHERE `category_id` = ?', [1]);
        #使用命名绑定
        #除了使用 ? 表示参数绑定外,你还可以使用命名绑定的形式来执行一个查询:
        return DB::select('select * from `article` where `id` = :id', ['id' => 2]);

        return DB::insert('insert into `article` (`title`, `content`) values (?, ?)', ['aa', 'aaa']);
        return DB::update('update `article` set `views` = 100 where id = ?', [66]);
        return DB::delete('delete from `article` where id = ?', [66]);

        #自动提交
        return DB::transaction(function () {
            DB::select('select * from `article` where `id` = :id', ['id' => 65]);
            DB::update('update `article` set `views` = 100 where id = ?', [65]);
        });
        #手动提交
        DB::beginTransaction();
        DB::rollBack();
        DB::commit();

        #处理死锁
        DB::transaction(function () {
            DB::update('update users set votes = 1');
            DB::delete('delete from posts');
        }, 5);

https://learnku.com/docs/laravel/9.x/database/12245

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档