Support the ongoing development of Laravel.io →
Database Eloquent

I have 3 tables:

id | name

1       |      Men >80kg   

2       |      Women >80kg   

3       |      Team Men >80kg   

Category_Tournament (List all categories available in a tournament)

id | category_id | tournament_id

1 | 1 | 2

2 | 2 | 2

3 | 3 | 2

4 | 4 | 2

Category_Tournament_User (categories registered by user)

id | category_tournament_id | user_id

1 | 1 | 201

2 | 3 | 202

Category is the list of categories for all tournaments (C for abreviation)

Category_Tournament is the list of categories the admin configured for this tournament (CT for abreviation)

Category_tournament_User is the categories the user has registred (CTU for abreviation)

User has a list of checkbox that I need to sync with:

        $tournament->categories_user()->sync($categories);

In Tournament Model, I have:

public function categories_user()
{
    return $this->belongsToMany('App\Category','category_tournament_user', 'user_id', 'category_tournament_id')
        ->withTimestamps();
}

Thing is data is syncing in CTU, but instead of inserting user_id, it insert tournament_id.

It probably does it because I'm in Tournament Model.

I tried to put the relationship in User Model, but couldn't make it work neither

Last updated 3 years ago.
0

try this

$tournament->user_categories->sync($userCategories, [
    'category_id' => $category_id
    .....
    ....
]);
Last updated 9 years ago.
0

Thing is I can't define the last relation user_categories :(

0

I updates my question

0

Sign in to participate in this thread!

Eventy

Your banner here too?

xoco70 xoco70 Joined 31 Dec 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.