I tried to find it for a long time, and I cannot believe that Laravel does not have this functionality.
So, I can write:
select * from a join b where a.id = b.id
or more beautiful:
select * from a join b using(id)
The first case for Laravel is simple:
$query->leftJoin('b', 'a.id', '=', 'b.id')
But how to write the second case? I expect it to be simple and short, for example:
$query->joinUsing('b', 'id')
But there is no such method, and I cannot find it.
PS: it is possible that the answer is very simple, it is simply difficult to find by the word "use", because it is everywhere.
UPDATE
I will go deeper into the source, trying to make a scope or pass a function to connect, but even inside this function I canβt do 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 :)
}
So even if you forget about scope, I cannot do it in DB :: raw (), so this is not possible ... For the first time, I see that something is impossible in Laravel.