【问题标题】:Laravel: should I use ('foo', $bar) or ('foo', '=', '$bar') to query the database?Laravel:我应该使用 ('foo', $bar) 还是 ('foo', '=', '$bar') 来查询数据库?
【发布时间】:2013-07-29 05:58:17
【问题描述】:

使用 laravel eloquent 或 fluent 我可以使用 ('foo', $bar) 或 ('foo', '=', '$bar') 查询数据库

例如:

$var = DB::table('users')->where('foo', $bar)->get();

$var = DB::table('users')->where('foo', '=', $bar)->get();

据我了解,这两种方法都有效,但我的问题是:

我应该使用其中一种吗?为什么?

例如,我是否会遇到任何我不会遇到的问题?或者两者都可以安全使用吗?

【问题讨论】:

    标签: php laravel laravel-4 eloquent


    【解决方案1】:

    它们是一样的。从 Query Builder.php 文件中:

        // If the given operator is not found in the list of valid operators we will
        // assume that the developer is just short-cutting the '=' operators and
        // we will set the operators to '=' and set the values appropriately.
    

    换句话说,“=”是默认值,从 Laravel 的角度来看没有区别。

    您可能想要使用“=”的唯一原因是为了让浏览您的代码的其他开发人员更容易阅读,因为您已明确声明“foo 等于 boo”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-25
      • 2022-11-04
      • 2014-03-20
      • 1970-01-01
      • 1970-01-01
      • 2020-02-02
      • 2020-10-03
      相关资源
      最近更新 更多