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?