代码之家  ›  专栏  ›  技术社区  ›  Danish

通过透视表10月CMS从表中检索条目

  •  1
  • Danish  · 技术社区  · 9 年前

    有谁能帮我编写查询,从我的words表中检索单词,这样单词就可以通过类型透视表与类型模型建立归属关系了吗?

     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) Word::all() 返回集合中包含6项的数组。

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

    2 回复  |  直到 9 年前
        1
  •  0
  •   nik.parotikov    9 年前

    您可以提供透视表:

    public $belongsToMany = [                                                                                                                                                                                      
            'typeswb' => [                                                                                                                                                                                                
            'Yeoman\Wordbank\Models\Type',                                                                                                                                                                      
            'table'=>'yeoman_wordbank_types_pivotb',                                                                                                                                                               
            'key' =>'word_id ',                                                                                                                                                                                         
            'otherKey' => 'type_id ',                                                                                                                                                                                   
            'pivot'    => ['word_id ', 'type_id '],                                                                                                                                                                      
            ],                                                                                                                                                                                                         
    
        ];
    
        2
  •  0
  •   Danish    9 年前

    $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 这是定义 belongToMany whereHas read this 了解更多信息。基本使用 wherehas 我指示查询查找关系。然后,在函数闭包中,我使用我的类型表中的一个键进行查询,即 type_id 。有关类型表的键,请再次参阅我上面的问题。