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

clkline's avatar

How to map this relationship?

After a long break from Laravel in favor of learning my job's current system better, I have picked back up the "Project Flyer" tutorial as a basis for creating a very simple (to begin with) website for myself. I like to write, and I am building a place to showcase some of my stories. In theory this will also expand to drawings and anything else creative I might come up with, including website showcases (here-out called "medium" for the sake of this thread). But for now, it's just stories

Tables:

  • stories
  • categories -> lookup table
  • story_categories

The thought is that the categories table will house any categories that can be used for anything posted to the site (multimedia) - so if there is, say, a "thriller" category, I could add both stories and drawings to it. As medium of art are added, they will get their own mapping tables as well ("drawing_categories" would map from "drawings" table and "categories" table).

Any media can have multiple categories.

I am very knew to Eloquent Relationships, and I haven't got a clue how this should look. At my job we write the needed sql right into the models and don't use any outside relationship tools, so the concept of what Eloquent can do is foreign to me as well. I will take any advice!

0 likes
5 replies
vitorf7's avatar
vitorf7
Best Answer
Level 9

@clkline

It seems like what you are talking about in having story_categories and drawing_categories is a Many to Many. Something like:

class Story extends Model
{
    public function categories()
    {
        // Note that the 2nd argument is the name of the pivot table, 3rd argument id the column that matches the ID of this class, and the 4th argument the column that matches the ID of the other class
        return $this->belongsToMany('App\Category', 'story_categories', 'story_id', 'category_id');
    }
}

class Category extends Model
{
    public function stories()
    {
        // Note that the 2nd argument is the name of the pivot table, 3rd argument id the column that matches the ID of this class, and the 4th argument the column that matches the ID of the other class
        return $this->belongsToMany('App\Story', 'story_categories', 'category_id', 'story_id');
    }
}

However Polymorphic relationships are better if any of those categories can be used by any other class. I am currently working on a project I have a categories table but I decided to have a type column so that some would be only for news stories, others for case studies etc and then used the pivot table approach as you mentioned. If you want ANY of the categories to be used by ANY class then polymorphic relationship is your friend

1 like
clkline's avatar

Thank you both! I just watched the video I was linked to. That is actually a pretty cool relationship idea! I will give this a try :)

clkline's avatar

I am finding that my method needs to be mirrored of the video provided. So for every medium I add, I have to add a categorize_id and categorize_type to that table. I just want to make sure I'm understanding - is this correct?

EDIT: This also seems to break the "Any media can have multiple categories" desire as well. So I am going to try @vitorf7 instead

clkline's avatar

I have decided that I am confusing categories and tags and can separate out the functionality I have in my head :) So I will go polymorphic with the categories and, when I add tags later, figure out the pivot table then.

Please or to participate in this conversation.