【问题标题】:Building Complex Join Queries in Laravel在 Laravel 中构建复杂的连接查询
【发布时间】:2014-12-11 12:26:22
【问题描述】:

假设我有一个表格来存储我网站上的所有菜单,包括下拉选择字段中的选项,如下表所示。我希望从后台管理员轻松管理菜单。

注意:我正在缓存所有菜单以避免访问数据库。

Table 1: tbl_menus

id   name                                slug                              type
------------------------------------------------------------------------------------               
 1   Accounting / Audit / Tax            accounting-audit-tax              industry
 2   Administration & Office Support     administration-office-support     industry
 3   Construction                        construction                      industry 
 4   Male                                male                              sex
 5   Female                              female                            sex
 6   Full Time                           full-time                         job-type
 7   Contract                            contract                          job-type
 8   Interim                             interim                           job-type                         

表 2:tbl_jobs

id   title                                job_type_menu_id            job_industry_menu_id                          
---------------------------------------------------------------------------------------------              
 1   System Administrator                 6                           1
 2   Solaris Admin                        6                           3
 3   IT Support Engeer                    7                           1 

我想要什么: 我正在尝试使用下面的查询将 tbl_menus 表连接到 tbl_jobs 表,以便访问 tbl_menus.name、tbl_menus.slug、tbl_menus.type 等菜单单元

DB::table('tbl_menus')
 ->join('tbl_menus', 'tbl_jobs.job_type_menu_id',     '=', 'tbl_menus.id')
 ->join('tbl_menus', 'tbl_jobs.job_industry_menu_id', '=', 'tbl_menus.id')
 ->select('tbl_jobs.id', 'tbl_jobs.title', 'tbl_menus.name', 'tbl_menus.slug');

我已经尝试过这个查询并且它没有产生任何结果。

我相信您对我希望实现的目标有更清晰的了解。我们将不胜感激您的专家解决方案。

注意:我需要选择此菜单或将其加入工作的原因是为了使我能够执行以下操作:

@foreach($jobs as $job)
  {{$job->job_type->name}} prints name instead of printing just ID
  {{$job->industry->name}}
@endforeach

【问题讨论】:

  • put ->get() on the end :) 因为查询生成器是从选择返回的,而不是查询的实际结果,因为它没有被执行。
  • @MattBurrow 我将 get() 方法链接到最后。我知道问题来自我的查询,尤其是 JOIN 语句。我肯定做错了什么。只需要一个有效的查询。一样的感谢

标签: sql join laravel builder


【解决方案1】:

您似乎正试图加入与您正在使用的表相同的表。

DB::table('tbl_menus')
 ->join('tbl_menus', 'tbl_jobs.job_type_menu_id',     '=', 'tbl_menus.id')
 ->join('tbl_menus', 'tbl_jobs.job_industry_menu_id', '=', 'tbl_menus.id')
 ->select('tbl_jobs.id', 'tbl_jobs.title', 'tbl_menus.name', 'tbl_menus.slug');

试试这个

DB::table('tbl_menus')
 ->join('tbl_jobs', 'tbl_jobs.job_type_menu_id',     '=', 'tbl_menus.id')
 ->join('tbl_jobs', 'tbl_jobs.job_industry_menu_id', '=', 'tbl_menus.id')
 ->select('tbl_jobs.id', 'tbl_jobs.title', 'tbl_menus.name', 'tbl_menus.slug');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 2019-12-25
    • 1970-01-01
    • 2011-10-21
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多