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

musti132's avatar

Query Issue Laravel

I have this many to many (campaign_events), in this table I have these records

+----+----------+-------------+----------+
| id | event_id | campaign_id | group_id |
+----+----------+-------------+----------+
| 11 |        2 |           1 |        1 |
| 12 |        2 |           1 |        3 |
+----+----------+-------------+----------+

I created the relationship in the Campaign Model like so

    public function events()
    {
        return $this->belongsToMany(Event::class, 'campaign_event', 'campaign_id', 'event_id')
            ->using(CampaignEvent::class)
            ->withPivot('group_id');
    }

And use this query to get the Campaign and Event relationship together:

 $campaigns = Campaign::with(['pings.tracking', 'events'])->get();

The problem is when I get the records it returns the events 2 times like so:

{
            "id": 1,
            "name": "Test Campaign",
            "tag": "new-users",
            "description": "Welcomming new users to website",
            "start_datetime": "2022-01-24T11:15:05.000000Z",
            "end_datetime": "2022-01-29T11:15:05.000000Z",
            "events": [
                {
                    "id": 2,
                    "name": "Subscriber Added to Group",
                    "description": "Triggered when a subscriber is added to a group.",
                    "slug": "subscriber-added-to-group",
                    "group": [
                        {
                            "id": 1,
                            "name": "New Users",
                            "tag": "new-user",
                            "description": "Group for users who recently joined",
                            "created_at": "2023-03-08T09:49:52.000000Z"
                        },
                        {
                            "id": 3,
                            "name": "VIP Plan",
                            "tag": "vip-plan",
                            "description": "Group for users who is on VIP plan",
                            "created_at": "2023-03-08T12:07:09.000000Z"
                        }
                    ]
                },
                {
                    "id": 2,
                    "name": "Subscriber Added to Group",
                    "description": "Triggered when a subscriber is added to a group.",
                    "slug": "subscriber-added-to-group",
                    "group": [
                        {
                            "id": 1,
                            "name": "New Users",
                            "tag": "new-user",
                            "description": "Group for users who recently joined",
                            "created_at": "2023-03-08T09:49:52.000000Z"
                        },
                        {
                            "id": 3,
                            "name": "VIP Plan",
                            "tag": "vip-plan",
                            "description": "Group for users who is on VIP plan",
                            "created_at": "2023-03-08T12:07:09.000000Z"
                        }
                    ]
                }
            ],
            "state": "inactive",
            "pings": [],
            "created_at": "2 hours ago"
        }

What can I do to fix this?

0 likes
2 replies
Nakov's avatar

Have you tried getting the distinct entries:

public function events()
{
    return $this->belongsToMany(Event::class, 'campaign_event', 'campaign_id', 'event_id')
       ->distinct()
       ->using(CampaignEvent::class)
       ->withPivot('group_id');
}
musti132's avatar

@Nakov Yes I have tried the distinct, it didn't work, I ended up using another structure for the tables.

Please or to participate in this conversation.