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

bluesheep's avatar

Relationship problem

Hi all, I need your help!

Given I have some user types: teacher, students, workers and some departments: A, B, C.

One person is in one department and has one user type.

Now I want to create a blog post which all teachers from department A and students from B and C should see.

How do I setup and store and setup these relations?

I thought about a pivot table which holds the news_id, usertype_id and the department_id. And to get all posts for a person maybe a call like this: Post::teacher($department_id)->all()

But it would be great if I still could use the sync, detach, etc. methods to interact with the data.

Do you have any advice?

Just ask if you need more details :)

0 likes
8 replies
bluesheep's avatar

I forgot to say that all user types share the tame departments.

Each user type can be part of any department.

But as I said, a person has just one user type and one department at the same time.

SP1966's avatar

My wife says I should never give relationship advice.

2 likes
bluesheep's avatar

Maybe Post::teacher($department_id)->all() is to hard.

I have a repository. so maybe the call would be like $this->repo->filtered($usertype_id, $department_id)

I think the tables should look something like this:

Table: news

id      title           body
1       My Post     My Post Body
Table: usertypes

id      name    
1       teacher
2       student
3       worker
Table: departments

id      name    
1       A
2       B
3       C
Table: news_relations

id      news_id     usertype_id     department_id
1       1           2               2
2       1           1               1

But I have still no idea how to set up the relations and queries.

pmall's avatar

I can't wrap my head around your problem. Triple sided relationships are always hard to manage (in real life too @SP1966 ;) ).

You can try to introduce another model which has no real life meaning but which can turn your problem in a classical two sided relationship.

bluesheep's avatar
bluesheep
OP
Best Answer
Level 6

For all who have the same problem, that's my solution:

Models

// Usertype Model

class Usertype extends \Eloquent {

    //...

    public function news()
    {
        $model = $this->belongsToMany('News', 'news_relations')->withPivot('department_id');

        if($this->department_id) $model->wherePivot('department_id', '=', $this->department_id);

        return $model;
    }
}
// Department Model

class Department extends \Eloquent {

    //...

    public function usertypes()
    {
        return $this->belongsToMany('Usertype', 'news_relations')->withPivot('news_id');
    }

    public function news()
    {
        return $this->belongsToMany('News', 'news_relations')->withPivot('usertype_id');
    }
}
// News Model

class News extends \Eloquent {

    //...

    public function departments()
    {
        return $this->belongsToMany('Department', 'news_relations')->withPivot('usertype_id');
    }
}

Usage

Get all news for a specific department and all user types

Department::first()->news

Get all news for a specific user type and all departments

Usertype::first()->news

Get all news for a specific user type and department

Department::find(2)->usertypes()->find(2)->news

Add new relations

$relations = [
    ['department_id' => 2, 'usertype_id' => 2],
    ['department_id' => 1, 'usertype_id' => 2],
    ['department_id' => 3, 'usertype_id' => 3],
];
News::find(2)->departments()->sync($relations);
bluesheep's avatar

@JeffreyWay Maybe that's something for you eloquent series. just ignore my tries to solve that problem.

Please or to participate in this conversation.