我的user
课程中有以下方法:
/**
* Get all organisations for user (if owner)
*
* @param
*/
public function getOrganisationsOwned()
{
// If the user is owner of any one or many organisations then return this list
return Organisation::leftJoin('subscription_plans', 'organisations.subscription_plan_id', '=', 'subscription_plans.id')
->where('organisations.owner_id', '=', $this->id)
->select('organisations.*', 'subscription_plans.*')
->get();
}
该方法实质上是查询和连接两个表。每个表都有一个名为title
的列。
上面的输出根据需要使用正确的信息生成行,但只从右表(title
)返回一个subscription_plans
列,而不从列title
返回左表(organisations
)。我还注意到它也从一个表中删除了时间戳,因为它们具有相同的列名。
我理解
->select('organisations.*', 'subscription_plans.*')
会使查询返回两列。我错过了什么?新年快乐!
PS:以下是该集合的dd()
内容的副本,title
仅出现一次。
#attributes: array:44 [▼
"id" => 1
"title" => "Monthly Subscription"
"address_1" => "34 Florence Street"
"address_2" => ""
"suburb" => "Hornsby"
"state" => "NSW"
"postcode" => "2077"
"country_id" => 12
"currency_id" => 12
"time_zone_id" => 109
"phone" => "0392144497"
"website" => "http://www.Tremblay.com/est-aspernatur-et-ut-provident.html"
"business_id" => "82297955560"
"tax_registration" => 1
"logo" => "8aa656de-2bc2-4e14-dddd-e02fbcd2b76f"
"quote_terms_days" => 14
"invoice_terms_days" => 14
"fiscal_start_id" => 7
"industry_id" => 4
"company_size_id" => 3
"date_format_id" => 2
"date_time_format_id" => 20
"owner_id" => 1
"gateway_id" => "1"
"gateway_username" => "xxx"
"gateway_password" => "xxx"
"gateway_signature" => "xxx"
"gateway_accepted_cards" => "[1, 2, 3]"
"subscription_plan_id" => 1
"trial_ends_at" => "2015-11-07"
"grace_ends_at" => "2016-02-10"
"subscription_ends_at" => "2016-01-11"
"latitude" => "-33.70433500"
"longitude" => "151.10161900"
"registration" => "done"
"deleted_at" => null
"created_at" => "2016-01-01 14:59:47"
"updated_at" => "2016-01-01 14:59:47"
"amount" => "9.09"
"gst" => "0.91"
"gst_amount" => "10.00"
"billing_cycle" => "MONTH"
"trial_period_days" => 30
"grace_period_days" => 30
]
“缺失”title
列包含:
'title' => 'ABC Electrical'
答案 0 :(得分:1)
对于我的建议存在一些误解:您可以逐个列出字段名称,而不是使用*,并为2个标题字段提供别名。这并不意味着您应该保留'organisations.*', 'subscription_plans.*'
并将2个标题字段添加到带有别名的选择列表中,因为这样您可以选择两个标题字段两次,浪费内存和处理器时间。
您不应在选择列表中包含*表单,而是单独列出每个字段,其中2个标题字段标有别名:
public function getOrganisationsOwned()
{
// If the user is owner of any one or many organisations then return this list
return Organisation::leftJoin('subscription_plans', 'organisations.subscription_plan_id', '=', 'subscription_plans.id')
->where('organisations.owner_id', '=', $this->id)
->select('organisations.id', 'organisations.title AS org_title', ..., 'subscription_plans.subscription_plan_id', 'subscription_plans.title AS plan_title', ...)
->get();
}
是的,我知道,逐一列出这么多字段是***的痛苦,然而,每个字段只检索一次,很明显你正在获取所需的东西。
答案 1 :(得分:-1)
@ Shadow的建议有效,尽管我仍然感到困惑,因为我在每个*
前面添加了表名。以下是现在的工作:
public function getOrganisationsOwned()
{
// If the user is owner of any one or many organisations then return this list
return Organisation::leftJoin('subscription_plans', 'organisations.subscription_plan_id', '=', 'subscription_plans.id')
->where('organisations.owner_id', '=', $this->id)
->select('organisations.*', 'organisations.title AS org_title', 'subscription_plans.*', 'subscription_plans.title AS plan_title')
->get();
}