Maybe a use for polymorphic relations? I'm sure there was a video on here about them too - but doesn't seem to be showing up when I search :-/
Edit: ah-ha! https://laracasts.com/lessons/polymorphic-huh
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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:
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!
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
Please or to participate in this conversation.