在Laravel的'join'或'where'中使用'='有什么区别?
问题描述:
我用两种方式:在Laravel的'join'或'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();
,对我来说已经工作过的相同或者添加或删除=
迹象。 有人知道这是否允许?如果是这样,那么最好的办法是什么? 谢谢。
答
据源函数定义:
// 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.
所以你可以看到,如果你忽略了=
作为第二个参数,查询生成器将其置于默认情况下,这是一致的你描述的行为。
答
这很好,'='是查询生成器中的默认操作符。
请参阅https://github.com/laravel/framework/blob/5.5/src/Illuminate/Database/Query/Builder.php#L497了解“where”代码的来源。它假定如果存在2个参数,它就是等号运算符。
打我给它由*此*得多。有一个upvote。 – ceejayoz