我的 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/
文章目录git常用命令(简介,详细参数往下看)Git提交代码步骤gitpullgitstatusgitaddgitcommitgitpushgit代码冲突合并问题方法一:放弃本地代码方法二:合并代码常用命令以及详细参数gitadd将文件添加到仓库:gitdiff比较文件异同gitlog查看历史记录gitreset代码回滚版本库相关操作远程仓库相关操作分支相关操作创建分支查看分支:gitbranch合并分支:gitmerge删除分支:gitbranch-ddev查看分支合并图:gitlog–graph–pretty=oneline–abbrev-commit撤消某次提交git用户名密码相关配置g
当你在类中包含方法名冲突的模块时,它会使用类定义的方法。有没有办法选择我想运行的?moduleBdefself.hello"helloB"endendclassAincludeBdefself.hello"helloA"endendA.hello#=>thisprints"helloA",whatifIwant"helloB"? 最佳答案 Ben,当你在Ruby中调用一个方法(比如hello)时,会发生以下情况:如果接收者的特征类有一个名为hello的方法,它将被调用。如果不是:如果接收者的类有一个名为hello的实例方法,它将被调
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它
我已经安装了最新版本的compass、sass和susy。但我仍然收到此错误:Unabletoactivatesusy-2.1.1,becausesass-3.2.17conflictswithsass(~>3.3.0)有人知道这个Ruby是如何工作的吗?这是我安装的gem的列表:***LOCALGEMS***CFPropertyList(2.2.0)chunky_png(1.3.0)compass(0.12.4)compass-core(1.0.0.alpha.19)compass-import-once(1.0.4)compass-rails(1.1.3)fssm(0.2.10)l
我正在处理的代码库最近从Ruby1.9.2升级到Ruby1.9.3,从Rails3.1升级到Rails3.2.2。因为我使用的是RVM,所以我只是简单地执行了rvminstall1.9.3,我原以为这是所有必要的。当我运行时railss我得到了错误[BUG]cross-threadviolationonrb_gc()我找到了许多与此问题相关的链接。有oneonStackOverflow,但它并没有真正给出答案。最有希望的答案是ontheRVMsite:IneverycaseofthisIhaveseenthusfarithasalwaysendedupbeingthatarubygem
使用Sequelgem:employees=DB[:prm_master__employee.identifier].join(:prm_master__employee_custom_fields.identifier,:employee=>:employee).where("termination_date>=?","06/01/2012").or("termination_date=NULL").and("employee='holderl'")以上失败:~/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/sequel-3.
我们如何访问那些与byebug保留名称冲突的变量名称?(byebug)varlocalh={"hierarchyId"=>"59f0b029e4b037ef11a055f7","level"=>2,...self=(byebug)我想访问变量“h”但键入h会显示“byebug的帮助对话框”(byebug)hbreak--Setsbreakpointsinthesourcecodecatch--Handlesexceptioncatchpointscondition--Setsconditionsonbreakpointscontinue--Runsuntilprogramends,hi
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭10年前。我使用PHP的时间太长了,对它感到厌倦了。我也想学习一门新语言。我一直在使用Ruby并且喜欢它。我必须在Rails和Sinatra之间做出选择,那么您会推荐哪一个?Sinatra真的不能用来构建复杂的应用程序,它只能用于简单的应用程序吗?
在过去的C#和Java工作中,我习惯于这样的声明不是线程安全的:x+=y;但是,在与Ruby并行运行上述代码时,我未能观察到任何线程间的冲突。我读过Rubyautomaticallypreventsmultiplethreadsfromwritingtothesamedataconcurrently.这是真的?因此+=运算符在Ruby中是线程安全的吗? 最佳答案 好吧,这取决于您的实现和很多事情。在MRI中,有一个GVL(GiantVMLock)这样的东西,它控制哪个线程一次实际执行代码。你看,在MRI中,一次只有一个线程可以执行R
我很确定Ruby有这些(等同于__call、__get和__set),否则find_by将如何在Rails中工作?也许有人可以举一个简单的例子来说明如何定义与find_by相同的方法?谢谢 最佳答案 简而言之你可以映射__调用带有参数的method_missing调用__设置为方法名称以'='结尾的method_missing调用__获取不带任何参数的method_missing调用__调用PHPclassMethodTest{publicfunction__call($name,$arguments){echo"Callingob