简体   繁体   English

Laravel - 使用具有复合主键的表加入

[英]Laravel - Join with a table with composite primary key

My problem is to join 2 tables in Laravel framework. 我的问题是在Laravel框架中加入2个表。 One is dynamic name table (it's a variable) and second has composite primary key . 一个是动态名称表 (它是一个变量),第二个是复合主键 I have to use query builder instead of where(). 我必须使用查询生成器而不是where()。 Please view my following for details: 请查看以下详细信息:

I have 2 tables: 我有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`)
);

Now, I need to join 2 these tables, I use this code: 现在,我需要加入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();?>

It's quite simple to join these table, OK. 加入这些表非常简单,好的。 But my problem is the table name is dynamic. 但我的问题是表名是动态的。

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

?>

Please help me to solve this problem. 请帮我解决这个问题。 Many thanks!!! 非常感谢!!!

You need to import variables from the local scope to the anonymous function's scope, this is how: 您需要将变量从本地范围导入到匿名函数的范围,这是如何:

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

Notice the line: 注意这一行:

->join('links', function($join) use ($table)

Problem is the anonymous function doesn't know about the variable $table , so you tell it about the variable using use . 问题是匿名函数不知道变量$table ,所以你告诉它使用变量use

You can find it in the docs . 您可以在文档中找到它。

Please try : 请试试 :

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

?>

The problem was that the function doesn't see the $table variable inside it. 问题是该函数没有在其中看到$ table变量。 That's why you need to use the "use" statement . 这就是你需要使用“use”语句的原因。

Read more about anonymous functions in php here 在这里阅读有关php中匿名函数的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM