php Laravel 内部连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41621486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Inner Join?
提问by xiimoss
I have a PHP backend that I'm currently changing over to use the Laravel framework. However, I'm not quite sure how the Laravel inner join works.
我有一个 PHP 后端,我目前正在将其转换为使用 Laravel 框架。但是,我不太确定 Laravel 内连接是如何工作的。
The SQL query i'm trying to move over to Laravel is:
我试图转移到 Laravel 的 SQL 查询是:
"SELECT leagues.league_name FROM leagues INNER JOIN countries on leagues.country_id = countries.country_id WHERE countries.country_name = '$country' "
Would someone be able to explain how Laravel Joins work?
有人能解释一下 Laravel Joins 是如何工作的吗?
Apologies if this question isn't suitable.
如果这个问题不合适,请见谅。
回答by Avram
It should be something like this (haven't tested):
它应该是这样的(尚未测试):
$leagues = DB::table('leagues')
->select('league_name')
->join('countries', 'countries.country_id', '=', 'leagues.country_id')
->where('countries.country_name', $country)
->get();
$leagues
will be instance of the Illuminate\Support\Collection
object, so you can iterate over it using foreach
for example.
$leagues
将是Illuminate\Support\Collection
对象的实例,因此您可以使用foreach
例如迭代它。
You can pass 5th argument to the join()
function which will specify type of join (default is "inner").
您可以将第 5 个参数传递给join()
将指定连接类型的函数(默认为“内部”)。
If you're using Eloquent and have "League" model then you can use join on the model too:
如果您正在使用 Eloquent 并拥有“League”模型,那么您也可以在模型上使用 join:
$leagues = League::select('league_name')
->join('countries', 'countries.country_id', '=', 'leagues.country_id')
->where('countries.country_name', $country)
->get();
In this case, $leagues
will be an instance of Illuminate\Database\Eloquent\Collection
which extends regular Laravel collections and gives you a bit more functionality than regular collections.
在这种情况下,$leagues
将是一个Illuminate\Database\Eloquent\Collection
扩展常规 Laravel 集合的实例,并为您提供比常规集合更多的功能。
However, there is even an easier way to write this without using joins:
但是,还有一种更简单的方法可以在不使用连接的情况下编写此代码:
$leagues = League::select('league_name')->whereHas('countries', function($query) use ($country) {
$query->where('country_name', $country);
})->get();
Note that in this example "countries" is not a table name but Eloquent relationship name, so you need to set up your relationships before using this approach.
请注意,在此示例中,“countries” 不是表名,而是Eloquent 关系名称,因此您需要在使用此方法之前设置您的关系。
Also, instead of using join, this example will use two queries, or a nested query, I'm not sure; but something like this: SELECT league_name FROM leagues WHERE country_id IN (SELECT id FROM countries WHERE country_name='$country')
此外,这个示例将使用两个查询或一个嵌套查询,而不是使用 join,我不确定;但像这样:SELECT league_name FROM leagues WHERE country_id IN (SELECT id FROM countries WHERE country_name='$country')
回答by user1669496
It's pretty straight-forward, maybe it will make more sense just to see your query using the query builder.
这非常简单,也许仅使用查询构建器查看您的查询会更有意义。
$results = DB::table('leagues')
->join('countries', 'leagues.country_id', '=', 'countries.country_id')
->where('countries.country_name', $country)
->get();
回答by shazim ali
In Laravel Eloquent you can do this all in very smart way.
在 Laravel Eloquent 中,您可以以非常聪明的方式完成这一切。
parent_table_model::with('relational_table_model')->get();