2015-12-31 143 views
0

Laravel关系我有3个表:具有枢轴表

类别(ID,姓名)

Category_Tournament(CATEGORY_ID,tournament_id) - >枢轴表 Category_Tournament_User(CATEGORY_ID,tournament_id,USER_ID,确认)

类别可用类别列表 Category_Tournament是管理员配置 Category_tournament_User是用户registred

类别类别列表10

要获得在比赛中的所有类别,我可以很容易地做到这一点:

tournament->categories 

定义在比赛模型belongsToMany关系

我不知道什么如何与最后一个表中定义的关系。

我需要的是几类用户点击,我可以运行类似:

tournament->user_categories->sync($userCategories) 

,我应该同步表Category_Tournament_User(与CATEGORY_ID,tournament_id,USER_ID)

什么最好的办法来实现它?

编辑:

型号锦标赛:

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(); 
} 

} 

型号类别

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

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


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

型号用户:

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'); 
} 


} 

回答

0

你有多对多的关系在这里之间和Category_Tournament,您应该仔细阅读文档Many To Many

+0

是的,但我的问题是Category_Tournament作为复合PK –

+0

多一点帮助,将不胜感激!我已经在这个问题上花费了2天时间,并且很难用2条线解决问题。 –

+0

好吧给OP添加模型。 –

0

我认为你不需要有Category_Tournament_User表。你不能在Laravel为它做一个Model。你只需要一个表user_tournament。你应该在migration定义关系(外键),像这样:

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(); 
}); 

,那么你可以使用此代码:

用户> tournaments->同步($ userCategories);