mardi 6 juin 2017

Can I use Laravel's eloquent hasManyThrough relationship for my specific case?

I'm trying to retrieve data from a table that I don't have a direct relationship with from the model I am currently using.

My data structure:

Table: posts

  • id - integer
  • title - string

Table: post_stacks

  • id - integer
  • post_id - integer
  • stack_id - integer

Table: stacks

  • id - integer
  • body - string
  • url - string

My eloquent model is from the Post.php (posts table) and i'm trying to get all the stacks associated to my post (from the stacks table). I want to declare my relationships only on Post.php and not my pivot table (post_stacks). I tried using hasManyThrough but i can't get it to work?

public function img()
    {
        return $this->hasManyThrough(\App\Stack::class, \App\PostStack::class, 'post_id', 'stack_id', 'id');
    }

Would anyone know how i can retrieve my desired data? Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire