mardi 1 août 2017

Eloquent ORM hasManyTrough

i have a hasManyTrough() relation in my database but could not get it to work with eloquent.

the relation in my database

category(id)
entry(id)
category_entries(category_id, entry_id)

i have 3 models

Category
  has_many CategoryEntries

Entry
  has_many CategoryEntries

CategoryEntry
  belongs_to Category
  belongs_to Entry

so every category has many entries and every entry has many categories.

in rails i would do the following

Category
  has_many CategoryEntries
  has_many Entries, through: :category_entries

i have created the following in eloquent

  CategoryEntry
    public function category(){
        return $this->belongsTo('App\Category');
    }
    public function entry(){
        return $this->belongsTo('App\Entry');
    }

  Category
    public function categoryEntries(){
        return $this->hasMany('App\CategoryEntry');
    }

  Entry
    public function categoryEntries(){
        return $this->hasMany('App\CategoryEntry');
    }


    public function categories()
    {
        return $this->hasManyThrough('App\Category', 'App\CategoryEntry', 'category_id', 'id');
    }

but this will create the following sql command:

select `entries`.*, `category_entries`.`category_id` from `entries` 
inner join `category_entries` on `category_entries`.`id` = `entries`.`entry_id`

this makes no sence. where is my mistake?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2tZWRYT
via IFTTT

Aucun commentaire:

Enregistrer un commentaire