Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.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 通过透视表Octobercms从表中检索条目_Php_Mysql_Laravel 5_Octobercms - Fatal编程技术网

Php 通过透视表Octobercms从表中检索条目

Php 通过透视表Octobercms从表中检索条目,php,mysql,laravel-5,octobercms,Php,Mysql,Laravel 5,Octobercms,有人能帮我写一个查询,从单词表中检索单词,这样单词就可以通过类型透视表和类型模型建立归属关系了吗 下面是Word.php中的关系 public $belongsToMany = [ 'typeswb' => [ 'Yeoman\Wordbank\Models\Type', 'table' => 'yeoman_wordbank_types_pivotb', 'order' => 'typesofwordbank'

有人能帮我写一个查询,从单词表中检索单词,这样单词就可以通过类型透视表和类型模型建立归属关系了吗

下面是Word.php中的关系

 public $belongsToMany = [
    'typeswb' => [
        'Yeoman\Wordbank\Models\Type',
        'table' => 'yeoman_wordbank_types_pivotb',
        'order' => 'typesofwordbank'
    ]
];
下面是类型表的外观

    mysql> select * from yeoman_wordbank_types;
+----+--------------------+--------------------+
| id | typesofwordbank    | slugoftypes        |
+----+--------------------+--------------------+
|  1 | Common Words       | common-words       |
|  2 | Common Expressions | common-expressions |
|  3 | Common Proverbs    | common-proverbs    |
|  4 | Common Idioms      | common-idioms      |
+----+--------------------+--------------------+
4 rows in set (0.00 sec)
mysql> select * from yeoman_wordbank_types_pivotb;
+---------+---------+
| word_id | type_id |
+---------+---------+
|      18 |       2 |
|       5 |       4 |
|       9 |       3 |
|      19 |       1 |
|      31 |       1 |
+---------+---------+
5 rows in set (0.00 sec)
数据透视表的类型是什么样的

    mysql> select * from yeoman_wordbank_types;
+----+--------------------+--------------------+
| id | typesofwordbank    | slugoftypes        |
+----+--------------------+--------------------+
|  1 | Common Words       | common-words       |
|  2 | Common Expressions | common-expressions |
|  3 | Common Proverbs    | common-proverbs    |
|  4 | Common Idioms      | common-idioms      |
+----+--------------------+--------------------+
4 rows in set (0.00 sec)
mysql> select * from yeoman_wordbank_types_pivotb;
+---------+---------+
| word_id | type_id |
+---------+---------+
|      18 |       2 |
|       5 |       4 |
|       9 |       3 |
|      19 |       1 |
|      31 |       1 |
+---------+---------+
5 rows in set (0.00 sec)
如您所见,
type\u id
连接到
words\u id
。其中,
类型标识
来自类型表
单词标识
来自单词表

我需要找到一种使用
types\u id
获取单词的方法

我试过跟随

// $query = Word::all();
// foreach ($query->typeswb as $typeswb) {
   // $queryy = $typeswb->pivot->type_id = 1;
// }
还有其他类似的组合,但都是徒劳的,奇怪的是,我得到了
Word::find(1)
null,而
Word::all()
返回集合中包含6项的数组


感谢您的阅读,我将非常感谢任何提示或帮助

您可以提供透视表:

public $belongsToMany = [                                                                                                                                                                                      
        'typeswb' => [                                                                                                                                                                                                
        'Yeoman\Wordbank\Models\Type',                                                                                                                                                                      
        'table'=>'yeoman_wordbank_types_pivotb',                                                                                                                                                               
        'key' =>'word_id ',                                                                                                                                                                                         
        'otherKey' => 'type_id ',                                                                                                                                                                                   
        'pivot'    => ['word_id ', 'type_id '],                                                                                                                                                                      
        ],                                                                                                                                                                                                         

    ];

您可以提供数据透视表:

public $belongsToMany = [                                                                                                                                                                                      
        'typeswb' => [                                                                                                                                                                                                
        'Yeoman\Wordbank\Models\Type',                                                                                                                                                                      
        'table'=>'yeoman_wordbank_types_pivotb',                                                                                                                                                               
        'key' =>'word_id ',                                                                                                                                                                                         
        'otherKey' => 'type_id ',                                                                                                                                                                                   
        'pivot'    => ['word_id ', 'type_id '],                                                                                                                                                                      
        ],                                                                                                                                                                                                         

    ];

好的,我解决了这个问题,通常是通过builder组件,这个功能很容易实现,但我不清楚如何在我自己的组件中实现。下面是我如何解决这个问题的:

$query = Word::whereHas('typeswb', function($q)
         {
             $q->where('type_id', '=', post('typesofwordsearch')); //post('typesofwordsearch') return integer which are id's of types according to the table
         })->limit(5)->get();
有关其工作原理的详细信息:

所以,可能还有另一种方法,但经过长时间的尝试,这个方法奏效了

首先,我使用我的单词模型,在单词模型中,我有
typeswb
,它定义了
与任何
关系的关系(参见问题)。我正在使用whereHas
了解更多信息。基本上,我使用wherehas
命令查询查找关系。之后,在函数闭包中,我使用类型表的一个键进行查询,即
type\u id
。有关表类型的键,请再次参阅上面的问题


希望这能帮助别人。干杯

好的,所以我解决了这个问题,通常是通过builder组件,这个功能很容易实现,但我不清楚如何在我自己的组件中实现。下面是我如何解决这个问题的:

$query = Word::whereHas('typeswb', function($q)
         {
             $q->where('type_id', '=', post('typesofwordsearch')); //post('typesofwordsearch') return integer which are id's of types according to the table
         })->limit(5)->get();
有关其工作原理的详细信息:

所以,可能还有另一种方法,但经过长时间的尝试,这个方法奏效了

首先,我使用我的单词模型,在单词模型中,我有
typeswb
,它定义了
与任何
关系的关系(参见问题)。我正在使用whereHas
了解更多信息。基本上,我使用wherehas
命令查询查找关系。之后,在函数闭包中,我使用类型表的一个键进行查询,即
type\u id
。有关表类型的键,请再次参阅上面的问题


希望这能帮助别人。干杯

你确定你在回答我的问题吗?我知道我可以提供额外的键,以防我想在pivot中使用不同的键名。但我问的是如何提问。你确定你在回答我的问题吗?我知道我可以提供额外的键,以防我想在pivot中使用不同的键名。但我问的是如何查询。您是否在
类型
模型上定义了
$belongstomy
?您好@Meysam,正如您在问题的开头所看到的,我定义了!不,我在您的
Word
模型中看到的是
$belongtomany
。我问的是
类型
模型。@Meysam不,我没有在
类型
模型上定义任何反向关系,因为我只是想让
属于我的
Word
模型上的许多类型关系。尽管如此,我还是解决了这个问题,看看我的答案:)干杯!嗯,我认为你在回答问题时是在重新发明轮子。在
类型
模型上定义反向关系将帮助您避免这种情况。您是否在
类型
模型上定义了
$belongstomy
?您好@Meysam,正如您在问题的开头所看到的,我定义了!不,我在您的
Word
模型中看到的是
$belongtomany
。我问的是
类型
模型。@Meysam不,我没有在
类型
模型上定义任何反向关系,因为我只是想让
属于我的
Word
模型上的许多类型关系。尽管如此,我还是解决了这个问题,看看我的答案:)干杯!嗯,我认为你在回答问题时是在重新发明轮子。在
类型
模型上定义反向关系将帮助您避免这种情况。