Php Laravel关系/连接不起作用
我可能忽略了与Elount的关系的要点,但是,如果我使用原始SQL,这将是我的问题Php Laravel关系/连接不起作用,php,laravel,eloquent,laravel-5,Php,Laravel,Eloquent,Laravel 5,我可能忽略了与Elount的关系的要点,但是,如果我使用原始SQL,这将是我的问题 SELECT [I have lots of columns I won't list them all here] FROM payments as t1 LEFT JOIN selected_postcodes_34894839483_1 as t2 ON t1.`Vendor ZIP` = t2.postcode 选定的邮政编码仅包含邮政编码列表 我的控制器是这样的 $temp_table_data
SELECT [I have lots of columns I won't list them all here]
FROM payments as t1
LEFT JOIN selected_postcodes_34894839483_1 as t2 ON t1.`Vendor ZIP` = t2.postcode
选定的邮政编码仅包含邮政编码列表
我的控制器是这样的
$temp_table_data = new TempTable;
$payments = $temp_table_data
->setTempTable("selected_postcodes_{$timestamp_string}_{$userid}")
->payment()->get()->toArray();
class Payment extends Model {
public function tempTable()
{
return $this->belongsTo('App\Models\TempTable', 'postcode', 'Vendor ZIP');
}
当时,我认为该查询的->payment()部分可能会返回所有相关的付款数据。我认为那是不对的,但我不知道还有什么好尝试的
我有这样的模特
$temp_table_data = new TempTable;
$payments = $temp_table_data
->setTempTable("selected_postcodes_{$timestamp_string}_{$userid}")
->payment()->get()->toArray();
class Payment extends Model {
public function tempTable()
{
return $this->belongsTo('App\Models\TempTable', 'postcode', 'Vendor ZIP');
}
及
我试着去看看这个诱人的模型是否能发挥其自身的作用——忘记了加入/关系
$data = $temp_table_data->setTempTable("selected_postcodes_{$timestamp_string}_{$userid}")->get()->toArray();
那确实有效。我得到了一个适当的回收。我还尝试过在供应商ZIP中删除空格,但没有任何帮助
---更新---
CREATE TABLE `selected_postcodes_1431076067_1` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`postcode` varchar(7) DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1581 DEFAULT CHARSET=latin1;
这就是我得到的错误
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'payments.Vendor ZIP' in 'where clause'
(SQL: select * from `payments` where `payments`.`Vendor ZIP` is null and `payments`.`Vendor ZIP` is not null)
——第二次更新---
CREATE TABLE `selected_postcodes_1431076067_1` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`postcode` varchar(7) DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1581 DEFAULT CHARSET=latin1;
我不是这个表的所有者-所以我可能无法更改字段名以删除空格(除非必要)
我看到的一个问题是,对于给定的代码,您在一个空的attentiable
对象上调用了payment()
。为了获取相关记录,您首先需要获取tentable
记录。要分解它,您已完成以下操作:
$temp_table_data = new TempTable;
// start with empty TempTable object
$payments = $temp_table_data
// set the table attribute on the empty object
->setTempTable("selected_postcodes_{$timestamp_string}_{$userid}")
// gets the payment relationship from the empty object
->payment()
// gets all the payments for an empty object
->get()
// converts collection to array
->toArray();
为了获得tentable
对象的相关付款,首先需要获得填充的tentable
对象。例如:
$temp_table = new TempTable;
$temp_table_data = $temp_table
->setTempTable("selected_postcodes_{$timestamp_string}_{$userid}")
->get();
// $temp_table_data is now a Collection of all the records in the given table
// loop through all the records and print out their payments
foreach($temp_table_data as $tt) {
print_r($tt->payment->toArray());
}
另外请注意,如果您想加快速度,可以在从临时表中获取记录时更改付款关系:
$temp_table_data = $temp_table
->setTempTable("selected_postcodes_{$timestamp_string}_{$userid}")
->with('payment') // eager load payment relationship
->get();
编辑
对有关快速加载的评论的回应
在这种情况下,由于必须在创建模型的新实例后设置表名,因此不能对模型调用with()
;您需要对使用实例化模型创建的查询对象进行深入研究并调用with()
。因此,您需要向方法链添加一个调用,而不是上面的代码:
$temp_table_data = $temp_table
->setTempTable("selected_postcodes_{$timestamp_string}_{$userid}")
->newQuery() // get a new query builder object for this instance
->with('payment') // eager load payment relationship on the query object
->get();
在模型上调用with()
时,它会创建模型的一个新实例,然后使用该新实例创建一个新的查询生成器对象。这种情况下的问题是模型的新实例将不会设置表名
编辑2
对有关选择特定列的评论的回应
如果要修改付款上使用的查询(例如仅选择特定列),则有两个选项,具体取决于您是否渴望加载
如果不是即时加载,您只需像任何其他查询一样修改付款查询:
foreach($temp_table_data as $tt) {
// note the use of the relationship query (payment()), not the Collection attribute (payment)
print_r($tt->payment()->select('Input RAC', 'Contract Number', 'Foreign Ind')->toArray());
}
如果急加载,可以修改用于急加载数据的查询。在with()
方法中,将关系数组传递给eager load,其中键是关系的名称,值是用于修改关系查询的闭包。这里需要注意的一点是,如果修改select子句,则必须将包含外键的列包含到TESTERABLE表中,否则Laravel将不知道如何链接正在创建的对象
$temp_table_data = $temp_table
->setTempTable("selected_postcodes_{$timestamp_string}_{$userid}")
->newQuery() // get a new query builder object for this instance
->with(['payment' => function ($query) {
// this Closure contains the query object for the payment relationship
// make sure your select includes the foreign key
$query->select('Vendor ZIP', 'Input RAC', 'Contract Number', 'Foreign Ind');
}]) // eager load payment relationship on the query object
->get();
你能添加你的db表结构吗?我没有看到Vendor-ZIP
列。仅供应商代码
。这可能是问题所在吗?就在bottom@mike3875这是否有效,或者您是否遇到错误:$data=$temp\u table\u data->setTempTable(“选定的邮政编码”{$timestamp\u string}}{$userid}”)->with('payment')->get()->toArray()代码>与问题无关,但请考虑为您的DB字段和方法使用适当的命名约定。从长远来看,这将使事情变得更容易。关于这个问题,;正如patricus所说,您试图在一个空模型上获取一个关系。顺便说一句,您的反向关系(belongsTo)将不起作用,因为没有设置正确的表,Laravel将默认为temp\u table
,谢谢。尝试快速加载SQLSTATE[HY000]时出错:一般错误:1096未使用表(SQL:select*)
。你知道为什么吗?另外,我如何使用payment
仅从payments表中选择某些行(因为它是一个如此大的表)。我尝试了print\r($tt->payment('Input RAC','Contract Number','Foreign Ind')->toArray()代码>但是运气不好..@mike3875我已经更新了我的答案以回应你的评论。太棒了。谢谢我的阵型有点疯狂。有很多多个阵列,而不仅仅是一个巨大的多阵列,每个阵列似乎都有随机数量的子阵列。奇怪的