简体   繁体   English

在Laravel的'join'或'where'中使用'='有什么区别吗?

[英]Is there any difference between using '=' in a 'join' or 'where' on Laravel?

I have used both ways: 我已经使用了两种方式:

$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();

and: 和:

$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();

and for me it has worked the same either adding or removing the = sign. 对我来说,无论添加还是删除=符号,它的工作原理都是相同的。 Does anybody know if this is allowed? 有人知道是否允许这样做吗? If so, what's the best way to do it? 如果是这样,最好的方法是什么? Thanks. 谢谢。

According to the function definition in the source: 根据源代码中的函数定义:

// 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.

So you can see, if you omit the = as the second argument, the query builder will place it there by default, which is consistent with the behavior you describe. 因此,您可以看到,如果省略=作为第二个参数,则查询构建器默认会将其放置在此处,这与您描述的行为一致。

Reference 参考

This is fine, the '=' is the default operator in the query builder. 很好,'='是查询构建器中的默认运算符。

See https://github.com/laravel/framework/blob/5.5/src/Illuminate/Database/Query/Builder.php#L497 for source of the 'where' code. 请参阅https://github.com/laravel/framework/blob/5.5/src/Illuminate/Database/Query/Builder.php#L497以获取“ where”代码的来源。 It assumes that if 2 arguments exist, it is the equals operator. 它假定如果存在2个参数,则它是equals运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM