Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel-与具有复合主键的表连接_Php_Mysql_Join_Laravel - Fatal编程技术网

Php Laravel-与具有复合主键的表连接

Php Laravel-与具有复合主键的表连接,php,mysql,join,laravel,Php,Mysql,Join,Laravel,我的问题是在Laravel框架中连接两个表。一个是动态名称表(它是一个变量),第二个是复合主键。我必须使用查询生成器而不是where()。有关详细信息,请查看我的以下内容: 我有两张桌子: CREATE TABLE `details` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `source_id` int(10) unsigned NOT NULL, `brand_id` int(10) DEFAULT NULL, PRI

我的问题是在Laravel框架中连接两个表。一个是动态名称表(它是一个变量),第二个是复合主键。我必须使用查询生成器而不是where()。有关详细信息,请查看我的以下内容:

我有两张桌子:

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`)
);
现在,我需要连接两个表,我使用以下代码:

<?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();?>

连接这些表很简单,好吗。但我的问题是表名是动态的

<?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();

?>

请帮我解决这个问题。 非常感谢

请尝试:

<?php 
$type = Input::get('type', null);
$table = $type . '_details';
$joinFunction = function($join) use ($table)
                {
                    $join->on($table . '.source_id', '=',  'links.source_id');
                    $join->on($table . '.brand_id','=', 'links.brand_id');
                }
$results =  \DB::table($table)
                ->join('links',$joinFunction )
                ->get();

?>

问题是函数没有看到其中的$table变量。这就是为什么需要使用“use”语句


阅读更多关于

您需要将变量从本地作用域导入匿名函数的作用域,具体操作如下:

$results =  \DB::table($table)
                ->join('links', function($join) use ($table)
                {
                    $join->on($table . '.source_id', '=',  'links.source_id');
                    $join->on($table . '.brand_id','=', 'links.brand_id');
                })
                ->get();
注意这一行:

->join('links', function($join) use ($table)
问题是匿名函数不知道变量
$table
,所以您可以使用
use
来告诉它变量


您可以在中找到它。

@Kame没问题,我开始学习PHP时也遇到过同样的问题。:)