dsplos5731 2014-08-30 05:58
浏览 30

Laravel离开加入AS

So I'm trying to get a Laravel Eloquent query to work. Here's the scenario:

1) I have one table called c5_wall_route_taxonomies That looks like the following:

---------------------------------------------
| ID   |  TYPE       |   CREATED_ON         |
---------------------------------------------
| 2    | 'country'   |  2014-08-12 12:12:12 |
---------------------------------------------

Where ID is an auto-incrementing integer, TYPE is an enum, and CREATED_ON is a pretty standard timestamp.

2) I have another table called c5_wall_route_taxonomy_options that looks like the following:

-----------------------------------------------------------------
| ID   |  WALL_ROUTE_TAXONOMY_ID   |  META_KEY   | META_VALUE   |
-----------------------------------------------------------------
| 1    | 2                         | 'label'     | 'Malaysia'   |
| 2    | 2                         | 'code'      | 'MY'         |
-----------------------------------------------------------------

And I'm trying to get the fields from the c5_wall_route_taxonomy table along with an extra 'country_label' field.

$result = DB::table('wall_route_taxonomies')
            ->join('wall_route_taxonomy_options as country', function($join) {
                $join->on('wall_route_taxonomies.id', '=', 'country.wall_route_taxonomy_id')
                     ->where('country.meta_key', '=', 'label');
            })->get();

Note that my table prefix is c5_.

Laravel tells me that it's unable to find c5_country.wall_route_taxonomy_id.

Now in this scenario, I could always just opt not to use the alias, and just use wall_route_taxonomy_options.wall_route_taxonomy_id instead, but what if I wanted to get both the label and the code from the options table as individual fields in my output?

Surely there's a way to implement this in Laravel?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 网络设备配置与管理这个该怎么弄
    • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
    • ¥20 西门子S7-Graph,S7-300,梯形图
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题
    • ¥15 matlab不知道怎么改,求解答!!
    • ¥15 永磁直线电机的电流环pi调不出来
    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥20 docker里部署springboot项目,访问不到扬声器