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');
}
是的,那工作,非常感谢。 – 2015-02-10 02:18:11