Laravel DB::insert() 和 DB::table()->insert() 的区别 [英] Difference between Laravel DB::insert() and DB::table()->insert()

查看:45
本文介绍了Laravel DB::insert() 和 DB::table()->insert() 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图弄清楚何时使用哪一个,以及是否应该同时使用.

I've been trying to figure out which one to use when, and if I should even use both.

一直在查看 Laravel 文档,它们都在里面.据我所知, DB::insert() 提供了比 DB::table()->insert() 更多的定制"查询.

Been looking at Laravel docs and they have both in there. From what I can make out of it, DB::insert() provides more "tailored" query than DB::table()->insert() does.

在如何以及何时使用哪个方面,谁能澄清两者的确切区别?

Would anyone be able to clarify what exactly the difference is in the two when it comes to how and when to use which?

推荐答案

  • DB::insert() 用于 原始sql 查询.示例:

    DB::insert('insert into users (id, name) values (?, ?)', [1, 'Dayle']);

    DB::table()->insert() 用于 查询生成器.示例:

    DB::table('users')->insert(['电子邮件' =>'john@example.com', '投票' =>0]);

    查询生成器将条件编译为原始 sql 查询,但我使用它是因为它更方便.

    Query builder compiles conditions to raw sql query, but I am using it because it is much more convenient.

    这篇关于Laravel DB::insert() 和 DB::table()->insert() 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆