【问题标题】:Is there any difference between using '=' in a 'join' or 'where' on Laravel?在 Laravel 的 'join' 或 'where' 中使用 '=' 有什么区别吗?
【发布时间】:2018-02-23 23:17:25
【问题描述】:

两种方式我都用过:

$this->data = DB::table('projects')
     ->select('companies_info.*', 'project_roles_types.name AS project_role_name')
     ->join('project_companies_members', 'project_companies_members.project_id', 'projects.project_id')
     ->where($some_variable, $project_id)
     ->get();

和:

$this->data = DB::table('projects')
    ->select('companies_info.*', 'project_roles_types.name AS project_role_name')
    ->join('project_companies_members', 'project_companies_members.project_id', '=', 'projects.project_id')
    ->where($some_variable, '=', $project_id)
    ->get();

对我来说,添加或删除 = 符号的效果相同。 有人知道这是否允许吗?如果是这样,最好的方法是什么? 谢谢。

【问题讨论】:

    标签: php laravel join laravel-5 where


    【解决方案1】:

    这很好,'=' 是查询生成器中的默认运算符。

    请参阅https://github.com/laravel/framework/blob/5.5/src/Illuminate/Database/Query/Builder.php#L497 以获取“where”代码的来源。它假定如果存在 2 个参数,则它是等于运算符。

    【讨论】:

      【解决方案2】:

      根据源码中的函数定义:

      // Here we will make some assumptions about the operator. If only 2 values are
      // passed to the method, we will assume that the operator is an equals sign
      // and keep going. Otherwise, we'll require the operator to be passed in.
      

      所以你可以看到,如果你省略=作为第二个参数,查询生成器会默认放置在那里,这与你描述的行为是一致的。

      Reference

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-12
        • 2014-02-07
        • 1970-01-01
        • 2016-08-23
        • 1970-01-01
        相关资源
        最近更新 更多