Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

php - 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.

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:

$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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So, the answer is - it's impossible.

Laravel doesn't support this functionality, which is really sad.

I fix it in Laravel source code, my PULL REQUEST here - https://github.com/laravel/framework/pull/12773


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...