【问题标题】:Retrieve entries from table via pivot table Octobercms通过数据透视表 Octobercms 从表中检索条目
【发布时间】:2017-06-30 11:08:16
【问题描述】:

谁能帮助我编写查询以从我的单词表中检索单词,使单词通过类型数据透视表与 Type 模型具有 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_id 连接到 words_id。其中types_id's 来自类型表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个项目的数组。

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

【问题讨论】:

  • 您是否在您的Types 模型上定义了$belongsToMany
  • 嗨@Meysam,正如你在问题的开头看到的那样,我做到了!
  • 不,我在您的Word 模型中看到的是$belongsToMany。我在问Types 模型。
  • @Meysam 不,我没有在Types 模型上定义任何反比关系,因为我只是想在我的Word 模型上拥有belongToMany 类型的关系。虽然,我已经解决了这个问题,请参阅我的答案:) 干杯!
  • 好吧,我认为您正在重新发明轮子。在 Types 模型上定义反比关系将帮助您避免这种情况。

标签: php mysql laravel-5 octobercms


【解决方案1】:

您可以提供数据透视表:

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

    ];

【讨论】:

  • 你确定,你在解决我的问题吗?我知道我可以提供额外的键,以防我想在我的数据透视表中使用不同的键名。但我问的是如何查询。
【解决方案2】:

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

$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 我正在指示查询查找关系。之后,在函数闭包中,我使用我的类型表中的一个键进行查询,即type_id。对于类型表的键,请再次参阅我上面的问题。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 2020-01-23
    • 2020-04-19
    • 2015-09-02
    • 2021-01-22
    • 1970-01-01
    相关资源
    最近更新 更多