使用Laravel-ManyToMany关系中的数据数组生成一个变量


Make a variable with array of data from Laravel ManyToMany relationship

我很难理解Laravels关系的用法。我终于用这个模型和这个代码保存了人和节日之间的关系:

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

这对我的个人节日数据库写得很好。

id       festival_id   person_id
0        1             1
1        1             2

但我的PersonFestival模型中没有任何代码,是吗?如何在一个可以在视图中使用blade的foreach的变量中检索id为1的节日的所有人?很抱歉这一团糟,这让我很困惑,但我觉得我即将实现它。

表格:

  • 个人(id,firstname,lastname)
  • 节日(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表(您的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