2015-02-10 36 views
0

我正在努力理解Laravel的关系使用情况。我终于设法拯救人与节之间的关系,利用模型和验证码:使用来自Laravel ManyToMany关系的数据阵列做一个变量

$person = new Person; 
$person->firstname = $firstname; 
$person->lastname = $lastname; 
$person->save(); 
$person_id = $person->id; 
$person->festival()->attach($festival_id); 

但我不知道如何使一个变量节日IM当前工作的所有人员。我在会话中存储festival_id的值:

$festival_id = Session::get('festival'); 

这对我的personFestival数据库写入罚款。

id  festival_id person_id 
0  1    1 
1  1    2 

但我没有任何代码在我PersonFestival模型,我应该吗? 我怎样才能检索ID = 1节的所有人在一个变量,我可以使用刀片的foreach在一个视图?对此感到抱歉,这对我来说很困惑,但我觉得即将实现它。

表:

  • 人(ID,名字,姓氏)
  • 节日(ID,姓名)
  • personFestival(ID,为person_id,festival_id)

型号:

class Festival extends Eloquent implements UserInterface, RemindableInterface { 

use UserTrait, RemindableTrait; 

protected $fillable = array(
    'name','year','info','slug', 'image','created_by','updated_by' 
); 

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

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

public function bands() { 
    return $this->hasMany('Band'); 
} 

public function persons() { 
    return $this->belongsToMany('Person','fs_festival_persons','person_id','festival_id'); 
} 
} 

class Person extends Eloquent implements UserInterface, RemindableInterface { 

use UserTrait, RemindableTrait; 

protected $fillable = array(
    'email','firstname','lastname','homepage', 'info','tlf','isonline','isbanned','username','password','password_temp','remember_token','code','active','created_by','updated_by' 
); 

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

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

public function festival() { 
    return $this->belongsToMany('PersonFestival','fs_festival_persons','person_id', 'festival_id'); 
} 
} 

class PersonFestival extends Eloquent implements UserInterface, RemindableInterface { 

use UserTrait, RemindableTrait; 

protected $fillable = array(
    'person_id','festival_id' 
); 

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

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

} 

回答

2

数据透视表(您的fs_festival_persons表格)通常不会有与其关联的模型,除非您确实需要一些特殊的逻辑。在这种情况下,它看起来不像你,所以你可能只是摆脱了这种模式。

为了回答您的其他问题,都与节日相关的人都可以通过你的节日模型之间的关系进行访问:

$festival = Festival::find(1); 

// Collection of Person objects via lazy loading 
$persons = $festival->persons; 

// Relationship object: 
$relation = $festival->persons(); 

// Manually getting the Persons through the relationship: 
$persons = $festival->persons()->get(); 

// You can iterate the Collection just like an array: 
foreach ($persons as $person) { 
    var_export($person->name); 
} 

你可以阅读更多关于这里查询的关系:Laravel 4.2/Laravel 5.0

+0

是的,那工作,非常感谢。 – 2015-02-10 02:18:11