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

时间:2017-09-14 20:58:45

标签: php laravel join laravel-5 where

我用过两种方式:

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

对我而言,添加或删除=符号的工作方式相同。 有人知道这是否被允许?如果是这样,最好的方法是什么? 感谢。

2 个答案:

答案 0 :(得分:5)

根据源中的函数定义:

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

答案 1 :(得分:4)

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

有关'其中'的来源,请参阅https://github.com/laravel/framework/blob/5.5/src/Illuminate/Database/Query/Builder.php#L497。码。它假设如果存在2个参数,则它是等于运算符。