通过 pivot table Octobercms 从 table 中检索条目

Retrieve entries from table via pivot table Octobercms

任何人都可以帮助我编写查询以从我的单词 table 中检索单词,使单词通过类型枢轴 table 与类型模型具有 belongsToMany 关系吗?

以下是 Word.php

中的关系
 public $belongsToMany = [
    'typeswb' => [
        'Yeoman\Wordbank\Models\Type',
        'table' => 'yeoman_wordbank_types_pivotb',
        'order' => 'typesofwordbank'
    ]
];

下面是类型 table 的样子

    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)

以及类型 pivot table 的样子

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_id 已连接到 words_id。其中 types_id's 来自 类型 tablewords_id's 来自 word table.

我需要找到一种使用 types_id 获取单词的方法。

我试过关注

// $query = Word::all();
// foreach ($query->typeswb as $typeswb) {
   // $queryy = $typeswb->pivot->type_id = 1;
// }

和其他一些类似的组合,但都是徒劳的,奇怪的是我得到 Word::find(1) null 而 Word::all() return 集合中有 6 个项目的数组。

感谢您的阅读,我将不胜感激任何提示或帮助。

您可以提供枢轴 table:

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

    ];

好的,所以我通常通过构建器组件解决了这个问题,这个功能很容易实现,但有点不清楚如何在我自己的组件中自己完成。所以,这就是我解决这个问题的方法:

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

关于其工作原理的分解:

所以,可能还有另一种方法,但在尝试了很长时间之后,这个方法奏效了。

首先,我使用我的 Word 模型,在 word 模型中,我有 typeswb 定义 belongToMany 关系(参见问题)。我正在使用 whereHas read this 来获取更多信息。基本上使用 wherehas 我指示查询查找关系。之后,在函数闭包中,我使用我的类型 table 中的一个键进行查询,即 type_id。对于 table 类型的键,请再次查看我上面的问题。

希望这对某人有所帮助。干杯!