Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TxNuno's avatar
Level 13

Help Figuring out the Proper Relationship

I have three tables.

exercises

  • id

invites

storytellers

  • storytellerable_id == exercise->id
  • storytellerable_type == App\Exercise
  • email == [email protected]

I need to be able to grab storyteller's invite($storyteller->invite) or a storyteller that belongs to an invite($invite->storyteller). I need to match the email, exercise ID, and the type.

I cannot wrap my head around how to make this work. Can someone help me out?

0 likes
2 replies
morteza's avatar
class Invite extends Model
{
    /**
     * Get all of the owning invitable models.
     */
    public function invitable()
    {
        return $this->morphTo();
    }
}

class Storyteller extends Model
{
    /**
     * Get all of the owning storytellerable models.
     */
    public function storytellerable()
    {
        return $this->morphTo();
    }
}

class Exercise extends Model
{
    /**
     * Get all of the exercise's invites.
     */
    public function invites()
    {
        return $this->morphMany('App\Invite', 'invitable');
    }

    /**
     * Get all of the exercise's storytellers.
     */
    public function storytellers()
    {
        return $this->morphMany('App\Storyteller', 'storytellerable');
    }
}


Corban's avatar

You should make something more logical to explain, like there's a storyteller that can send invites to ohter storytellers and things like that, that should help both you and us to come with an idea.

If I understand correctly, exercises are both storytellers and invites? I can't understand 100% what you are actually trying to relate.

Please or to participate in this conversation.