使用来自 Laravel 多对多关系的数据数组创建一个变量
Make a variable with array of data from Laravel ManyToMany relationship
我很难理解 Laravel 关系的用法。我终于设法保存了人和节日之间的关系,使用模型和这段代码:
$person = new Person;
$person->firstname = $firstname;
$person->lastname = $lastname;
$person->save();
$person_id = $person->id;
$person->festival()->attach($festival_id);
但我不确定如何与我目前正在参加的节日的所有人员一起制作变量。我将 festival_id 的值存储在会话中:
$festival_id = Session::get('festival');
我的 personFestival 数据库写得很好。
id festival_id person_id
0 1 1
1 1 2
但是我的 PersonFestival 模型中没有任何代码,对吗?
我怎样才能在我可以在视图中使用 blade 的 foreach 的变量中检索 id=1 的所有节日人员?很抱歉造成这个混乱,这让我很困惑,但我觉得我已经接近实现它了。
表格:
- 人(身份证、名字、姓氏)
- 节日 (id,name)
- 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');
}
Pivot tables(你的 fs_festival_persons table)通常没有与之关联的模型,除非你真的需要它们的一些特殊逻辑。在这种情况下,它看起来不像你那样,所以你可以去掉那个模型。
为了回答您的其他问题,可以通过节日模型上的关系访问与节日相关的所有人员:
$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
我很难理解 Laravel 关系的用法。我终于设法保存了人和节日之间的关系,使用模型和这段代码:
$person = new Person;
$person->firstname = $firstname;
$person->lastname = $lastname;
$person->save();
$person_id = $person->id;
$person->festival()->attach($festival_id);
但我不确定如何与我目前正在参加的节日的所有人员一起制作变量。我将 festival_id 的值存储在会话中:
$festival_id = Session::get('festival');
我的 personFestival 数据库写得很好。
id festival_id person_id
0 1 1
1 1 2
但是我的 PersonFestival 模型中没有任何代码,对吗? 我怎样才能在我可以在视图中使用 blade 的 foreach 的变量中检索 id=1 的所有节日人员?很抱歉造成这个混乱,这让我很困惑,但我觉得我已经接近实现它了。
表格:
- 人(身份证、名字、姓氏)
- 节日 (id,name)
- 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');
}
Pivot tables(你的 fs_festival_persons table)通常没有与之关联的模型,除非你真的需要它们的一些特殊逻辑。在这种情况下,它看起来不像你那样,所以你可以去掉那个模型。
为了回答您的其他问题,可以通过节日模型上的关系访问与节日相关的所有人员:
$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