gpt4 book ai didi

php - Laravel 5.2 Eloquent 列名称冲突

转载 作者:可可西里 更新时间:2023-11-01 09:04:04 26 4
gpt4 key购买 nike

我的 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'

最佳答案

对于我的建议存在一些误解:您可以一一列出字段名称并为 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();
}

是的,我知道,一个一个地列出这么多字段是 *** 的痛苦,但是,每个字段都被检索一次且仅一次,很明显您正在获取所需的内容。

关于php - Laravel 5.2 Eloquent 列名称冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34553474/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com