can anyone help me writing query to retrieve words from my words table in such way that words are having a belongsToMany relationship to Type model via types pivot table?
Here's how relationship looks like in Word.php
public $belongsToMany = [
'typeswb' => [
'Yeoman\Wordbank\Models\Type',
'table' => 'yeoman_wordbank_types_pivotb',
'order' => 'typesofwordbank'
]
];
Here is how types table looks like
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)
and wheres how types pivot table looks like
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)
As you can see type_id
are connected to words_id
. where types_id's
are from types table and words_id's
are from word table.
I need to find a way to get the words using types_id
.
I have tried following
// $query = Word::all();
// foreach ($query->typeswb as $typeswb) {
// $queryy = $typeswb->pivot->type_id = 1;
// }
and some other combination like that but all in vain, strangely I get Word::find(1)
null on this while Word::all()
return an array of 6 items in the collection.
Thank you for reading, I will appreciate any hint or help very much.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire