【发布时间】:2014-06-07 17:44:26
【问题描述】:
我的问题是在 Laravel 框架中加入 2 个表。一是动态名称表(它是一个变量),二是复合主键。我必须使用查询生成器而不是 where()。详情请查看我的以下内容:
我有 2 张桌子:
CREATE TABLE `details` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`source_id` int(10) unsigned NOT NULL,
`brand_id` int(10) DEFAULT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `links` (
`source_id` int(10) unsigned NOT NULL,
`brand_id` tinyint(3) unsigned NOT NULL DEFAULT '1',
PRIMARY KEY (`source_id`,`brand_id`)
);
现在,我需要加入 2 个这些表,我使用以下代码:
<?php $results = \DB::table('details')
->join('links', function($join)
{
$join->on('details.source_id', '=', 'links.source_id');
$join->on('details.brand_id','=', 'links.brand_id');
})
->get();?>
加入这些表很简单,OK。但我的问题是表名是动态的。
<?php
$type = Input::get('type', null);
$table = $type . '_details';
$results = \DB::table($table)
->join('links', function($join)
{
// the following code will show errors undefined $table
$join->on($table . '.source_id', '=', 'links.source_id');
$join->on($table . '.brand_id','=', 'links.brand_id');
})
->get();
?>
请帮我解决这个问题。 非常感谢!!!
【问题讨论】: