Laravel 5.使用USING运算符 [英] Laravel 5. Using the USING operator
问题描述
我试图找到它很长时间,而且我不敢相信Laravel没有此功能.
I tried to find it for a long time, and I can't believe that Laravel doesn't have this functionality.
所以,我可以写:
select * from a join b where a.id = b.id
或更漂亮:
select * from a join b using(id)
对于Laravel,第一种情况很简单:
First case is simple for Laravel:
$query->leftJoin('b', 'a.id', '=', 'b.id')
但是如何写第二种情况?我希望它应该简单而简短,例如:
But how to write second case? I expect that it should be simple and short, like:
$query->joinUsing('b', 'id')
但是没有这样的方法,我找不到它.
But thereis no such method and I can't find it.
PS:答案很可能很简单,因为很难找到使用"一词,所以很难找到答案,因为它无处不在.
PS: it's possible that the answer is very simple, it's just hard to find by word "using", because it's everywhere.
更新
我将更深入地探究源代码,尝试建立作用域或通过一个函数来联接,但是即使在此函数内部,使用此$ query也无法执行任何操作.示例:
I'm going deeper to source, trying to make scope or pass a function to join, but even inside of this function I can't to anything with this $query. Example:
public function scopeJoinUsing($query, $table, $field) {
sql($query->join(\DB::raw("USING(`{$field}`)")));
// return
// inner join `b` on USING(`id`) ``
// yes, with "on" and with empty quotes
sql($query->addSelect(\DB::raw("USING(`{$field}`)")));
// return
// inner join `b` USING(`id`)
// but in fields place, before "FROM", which is very logic :)
}
所以即使忘记范围,我也无法在DB :: raw()中做到这一点,所以这是不可能的...第一次我看到Laravel中有些不可能的事情.
So even if forget about scope , I can't do this in DB::raw() , so it's impossible... First time I see that something impossible in Laravel.
推荐答案
因此,答案是-这是不可能的.
So, the answer is - it's impossible.
Laravel不支持此功能,这实在令人遗憾.
Laravel doesn't support this functionality, which is really sad.
我已在Laravel源代码中对其进行了修复,请在此处进行我的请求- https://github.com /laravel/framework/pull/12773
I fix it in Laravel source code, my PULL REQUEST here - https://github.com/laravel/framework/pull/12773
这篇关于Laravel 5.使用USING运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!