[英]Laravel 5. Using the USING operator
I tried to find it for a long time, and I can't believe that Laravel doesn't have this functionality.找了半天,不敢相信Laravel没有这个功能。
So, I can write:所以,我可以写:
select * from a join b where a.id = b.id
or more beautiful:或更漂亮:
select * from a join b using(id)
First case is simple for Laravel:对于 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: it's possible that the answer is very simple, it's just hard to find by word "using", because it's everywhere. PS:答案可能很简单,只是很难用“使用”这个词找到,因为它无处不在。
UPDATE更新
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.我要更深入地了解来源,尝试制作 scope 或传递 function 加入,但即使在这个 function 内部,我也无能为力。 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 :)
}
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.因此,即使忘记 scope,我也无法在 DB::raw() 中执行此操作,所以这是不可能的……我第一次看到 Laravel 中的某些事情是不可能的。
So, the answer is - it's impossible.所以,答案是——这是不可能的。
Laravel doesn't support this functionality, which is really sad. Laravel 不支持这个功能,真的很可惜。
I fix it in Laravel source code, my PULL REQUEST here - https://github.com/laravel/framework/pull/12773我在 Laravel 源代码中修复它,我的 PULL REQUEST 在这里 - https://github.com/laravel/framework/pull/12773
Nothing is impossible in Laravel. Laravel 中没有什么是不可能的。
Until support for join using
is added to Laravel core, you can add support for it by installing this Laravel package: https://github.com/nihilsen/laravel-join-using . Until support for
join using
is added to Laravel core, you can add support for it by installing this Laravel package: https://github.com/nihilsen/laravel-join-using .
It works like this:它是这样工作的:
use Illuminate\Support\Facades\DB;
use Nihilsen\LaravelJoinUsing\JoinUsingClause;
DB::table('users')->leftJoin(
'clients',
fn (JoinUsingClause $join) => $join->using('email', 'name')
);
// select * from `users` left join `clients` using (`email`, `name`)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.