简体   繁体   English

Laravel 4查询生成器(联接)

[英]Laravel 4 Query Builder (Joins)

I have these tables in my database, namely "airport" and "route", the id of "airport" is a foreign key in "route" (ie Origin, Destination). 我的数据库中有这些表,即“ airport”和“ route”,“ airport”的ID是“ route”(即Origin,Destination)中的外键。

Airport
+-------+-------------+-----------------------+
|  id   | airportcode |  Location             | 
+-------+-------------+-----------------------+
|   1   |   CEB       | Cebu                  | 
|   2   |   MAN       | Manila                |
+-------+-------------+-----------------------+

Routes
+-------+-------------+-----------------------+
|   id  | Origin      |  Destination          | 
+-------+-------------+-----------------------+
|   1   |   1         | 2                     | 
|   2   |   2         | 1                     |
+-------+-------------+-----------------------+

So far, this is my query in my Controller and it's only returning the "Origin, Destination" 到目前为止,这是我在Controller中的查询,仅返回“ Origin,Destination”

DB::table('airport')
    ->join('route', 'airport.id','=','route.Origin')
    ->join('route', 'airport.id','=','route.Destination')
    ->select('route.Origin', 'route.Destination')
    ->get();

What I would like to do is this: 我想做的是这样的:

SELECT 'airport.Location' from airport, route WHERE 'route.Origin' = 'airport.id' AND 'route.Destination' = 'airport.id" . SELECT 'airport.Location' from airport, route WHERE 'route.Origin' = 'airport.id' AND 'route.Destination' = 'airport.id"

Any suggestions will do! 任何建议都可以!

So - you want to pull out the model for a specific airport id but only if it goes to the specified destination? 所以-您想为特定的机场ID提取模型,但仅在到达指定目的地时才提取模型?

Your first query will only return the two columns as that's what you told it to return 您的第一个查询将仅返回两列,因为这就是您告诉它返回的内容

You can get the airport easily by: 您可以通过以下方式轻松到达机场:

Airport::find($id);

Where $id is the id from a user input for example and should be the key. 其中$ id是来自用户输入的ID,例如,应为键。 Find will return a collection 查找将返回一个集合

You could also do: 您也可以这样做:

Airport::where('id','=', $id)->first() //will return the first record - you could also use ->get() to return a collection

Then if you have a join in your Airport model such as ->hasMany you could then do: 然后,如果您加入了机场模型,例如-> hasMany,则可以这样做:

Airport::where('id','=', $id)
    ->with('routes')
    ->get()

Which will return the airport with the related routes model attached to it 它将返回附带相关路线模型的机场

You can then take that a stage further and query the relationship by: 然后,您可以再进一步,并通过以下方式查询关系:

Airport::find($id)->routes()->where('destination','=',$dest_id);

I think that should do the trick - as long as you create the relationship correctly in the models 我认为应该可以解决问题-只要您在模型中正确创建关系

If you are using a select query make sure that you have mentioned all the fields you want... 如果使用选择查询,请确保已提及所有想要的字段...

it's only returning the "Origin, Destination" because you have mentioned only those two in your select query. it's only returning the "Origin, Destination"因为您在选择查询中仅提及了这两个。

try something like... 尝试类似...

DB::table('route')
   ->select('route.Origin', 'route.Destination','airport.Location')
   ->leftjoin('airport',  function($join)
                        {
                            $join->where('airport.id',array('route.Origin','route.Destination'));   
                         // I haven't used it, if any errors pls comment
                        })

   ->get();

hope this helps you... 希望这对您有帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM