php - 通过数据透视表 Octobercms 从表中检索条目

标签 php mysql laravel-5 octobercms

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

这是 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)

如您所见,type_idwords_id 相关联。其中 types_id's 来自 types 表words_id's 来自 word 表

我需要找到一种使用 types_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 '],                                                                                                                                                                      
        ],                                                                                                                                                                                                         

    ];

关于php - 通过数据透视表 Octobercms 从表中检索条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44845027/

相关文章:

php - 插入具有默认值的 Mysql 行

php - php 中的 html 出现乱码

MySQL相似表的索引使用不一致: 'Using index' and 'Using where; Using index'

laravel - 从 Eloquent 关系中选择特定列

css - 如何将样式表仅应用于 laravel 表单中的特定选择框?

php - 从 instagram 保存 instagram 高分辨率图像

javascript - 来自多维数组的全日历事件数据

mysql - 当有相似行时获取具有最大列值的行?

mysql - 在 xcode 应用程序启动时出现黑屏,那么如何以编程方式调用它?

javascript - Laravel 5.6 在公共(public)文件夹中添加 javascript 库 "Page not found"