jeudi 12 mars 2020

Laravel model relation within same table

I am working on a Laravel 5.7 project where I have three tables words, synonym_word and antonym_word.

words table has two columns id and word

synonym_word has three columns id, word_id and synonym_id

antonym_word has three columns id, word_id and antonym_id

I am storing all words, their synonyms and antonyms in Words table and referencing synonyms and antonyms in respective tables.

Word.php

class Word extends Model
{

    public function synonyms()
    {
        return $this->hasMany('App\Synonym');
    }

    public function antonyms()
    {
        return $this->hasMany('App\Antonym');
    }

}    

I am quering synonyms like this:-

$synonyms = Word::find(1)->synonyms;

The above query is giving all synonyms and their id's from Synonym table, but my actual Synonym word is stored in Words table.

How can I get Synonym words from Words table?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire