0
votes

I have 3 tables:

Category ( id, name)

Category_Tournament (category_id, tournament_id) --> pivot table Category_Tournament_User (category_id, tournament_id, user_id, confirmed)

Category is the list of available categories Category_Tournament is the list of categories the admin configured Category_tournament_User is the categories the user has registred

To get all categories in the tournament, I can do it easily with:

    tournament->categories

defining a belongsToMany relationship in tournament model

What I don't know how to define relationship with the last table.

What I need is the user click on several categories, and I can run something like:

    tournament->user_categories->sync($userCategories)

where I should sync table Category_Tournament_User ( with category_id, tournament_id, user_id)

What is the best way to achieve it???

EDIT:

Model Tournament:

class Tournament extends Model
{

protected $table = 'tournament';
public $timestamps = true;

protected $fillable = [
    'name',
    'date',
    'type',

];


/**
 * A tournament is owned by a user
 *
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function owner()
{
    return $this->belongsTo('App\User', 'user_id','id');
}

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */

public function categories()
{
    return $this->belongsToMany('App\Category')
                ->withTimestamps();
}

}

Model Category

class Category extends Model
{
protected $table = 'category';
public $timestamps = true;

protected $fillable = [
    'id',
    'name',
];


public function tournaments()
{
    return $this->belongsToMany('App\Tournament');
}
}

Model User:

class User extends Model implements AuthenticatableContract,  CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword, HasRole;

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users';

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
 protected $fillable = ['name','firstname','lastname','email', 'password','avatar',country_id','role_id',,'provider','provider_id','verified'];

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = ['password', 'remember_token'];



/**
 * Boot the model.
 *
 * @return void
 */
public static function boot()
{
    parent::boot();
    static::creating(function ($user) {
        $user->token = str_random(30);
    });
}

public function role()
{
    return $this->belongsTo('App\Role');
}

public function settings()
{
    return $this->hasOne('App\Settings');
}

public function invites()
{
    return $this->hasMany('App\Invite', 'email','email');
}

public function country()
{
    return $this->belongsTo('Webpatser\Countries\Countries');
}

/**
 * A user can have many tournaments
 *
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function tournaments()
{
    return $this->hasMany('App\Tournament');
}


}
2

2 Answers

0
votes

You have many to many relationship here between User and Category_Tournament and you should take a look in documentation exactly at Many To Many.

0
votes

I think you don't need to to have Category_Tournament_User table. and you can't make a Model for it in Laravel. you only need to a table user_tournament. and you should define relation(foreign key) on migration, like this:

Schema::create('user_tournament', function(Blueprint $table){
    $table->engine = 'InnoDB';
    $table->increments('id');
    $table->integer('user_id')->unsigned();
    $table->integer('tournament_id')->unsigned();
    $table->unique(['tournament_id', 'user_id']);//You can omit this
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
    $table->foreign('tournament_id')->references('id')->on('tournaments')->onDelete('cascade')->onUpdate('cascade');
    $table->nullableTimestamps();
});

then you can use this code:

user->tournaments->sync($userCategories);