Php 拉雷维尔:多对多关系
我在两张桌子(乐队和节日)之间有一种多对多的关系。创建一个乐队和一个节日是没有问题的。但是创建一个乐队并将其附加到节日,或者创建一个节日并将其附加到乐队。这就是我似乎有问题的地方 我不知道如何在Laravel中动态执行此操作。理想的做法是在创建乐队时选择一个节日、多个节日 解决方案应如下所示: 因此,在下拉列表中选择节日,band_id和festival_id存储在band_festival表中 节日模式:Php 拉雷维尔:多对多关系,php,laravel,laravel-4,controller,many-to-many,Php,Laravel,Laravel 4,Controller,Many To Many,我在两张桌子(乐队和节日)之间有一种多对多的关系。创建一个乐队和一个节日是没有问题的。但是创建一个乐队并将其附加到节日,或者创建一个节日并将其附加到乐队。这就是我似乎有问题的地方 我不知道如何在Laravel中动态执行此操作。理想的做法是在创建乐队时选择一个节日、多个节日 解决方案应如下所示: 因此,在下拉列表中选择节日,band_id和festival_id存储在band_festival表中 节日模式: <?php class Festival extends Eloquent {
<?php
class Festival extends Eloquent {
protected $guarded = array();
protected $primaryKey = 'festival_id';
public $timestamps = false;
public function bands(){
return $this->belongsToMany('Band', 'band_festivals', 'festival_id', 'band_id');
}
}
标注栏创建视图:
<h1>Create a Band</h1>
{{ HTML::ul($errors->all(),array('class' => 'errors')) }}
{{ Form::open(array('url' => 'bands')) }}
<div class="form-group">
{{ Form::label('Name') }}
{{ Form::text('band_name', '', array('class'=>'form-control', 'placeholder' => 'Name')) }}
</div>
<div class="form-group">
{{ Form::label('Members') }}
{{ Form::text('band_members', '', array('class'=>'form-control', 'placeholder' => 'Members')) }}
</div>
<div class="form-group">
{{ Form::label('Genre') }}
{{ Form::text('band_genre', '', array('class'=>'form-control', 'placeholder' => 'Genre')) }}
</div>
<div class="form-group">
{{ Form::label('Start Date') }}
{{ Form::text('band_startdate', '', array('class'=>'form-control', 'placeholder' => 'Start Date')) }}
</div>
<div class="form-group">
{{ Form::label('Festival') }}
{{ Form::text('festival_id', '', array('class'=>'form-control', 'placeholder' => 'Festival')) }}
</div>
{{ Form::submit('Create the Band!', array('class' => 'btn btn-succes')) }}
{{ Form::close() }}
迁移带:
public function up()
{
Schema::create('bands', function($table)
{
$table->increments('band_id');
$table->string('band_name');
$table->text('band_members');
$table->string('band_genre');
$table->dateTime('band_startdate');
});
}
public function down()
{
Schema::table('bands', function(Blueprint $table)
{
Schema::drop('bands');
});
}
移民节:
public function up()
{
Schema::create('band_festivals', function(Blueprint $table)
{
$table->integer('band_id')->unsigned()->index();
$table->integer('festival_id')->unsigned()->index();
});
}
public function down()
{
Schema::table('band_festivals', function(Blueprint $table)
{
Schema::drop('band_festivals');
});
}
在这种情况下,您非常接近:) 保存节日的逻辑应该是:
$festival_id = $input['festival_id'];
$band->festivals()->attach($festival_id);
尝试在关系中声明透视表名称,这应该可以完成以下工作:
class Band extends Eloquent {
protected $guarded = array();
protected $primaryKey = 'band_id';
public $timestamps = false;
public function festivals(){
return $this->belongsToMany('Festival', 'brand_festivals', 'brand_id', 'festival_id');
}
}
class Festival extends Eloquent {
protected $guarded = array();
protected $primaryKey = 'festival_id';
public $timestamps = false;
public function bands(){
return $this->belongsToMany('Band', 'brand_festivals', 'festival_id', 'brand_id');
}
}
对于您的组合,请尝试:
Form::select('festival_id', Festival::all()->lists('name', 'festival_id'));
希望这会有帮助。
这有点帮助,谢谢。然而,我并没有得到关于如何用节日id填充dropdownlist的答案,最好是用节日名称。因此,当我制作一个乐队时,我可以将其附加到一个节日,因此节日id和乐队表存储在Band_Festival表中。在Laravel中已经研究了一段时间了。对于您的组合,请尝试:Form::select('festival_id',festival::all()->list('name','festival_id');
$festival_id = $input['festival_id'];
$band->festivals()->attach($festival_id);
class Band extends Eloquent {
protected $guarded = array();
protected $primaryKey = 'band_id';
public $timestamps = false;
public function festivals(){
return $this->belongsToMany('Festival', 'brand_festivals', 'brand_id', 'festival_id');
}
}
class Festival extends Eloquent {
protected $guarded = array();
protected $primaryKey = 'festival_id';
public $timestamps = false;
public function bands(){
return $this->belongsToMany('Band', 'brand_festivals', 'festival_id', 'brand_id');
}
}
Form::select('festival_id', Festival::all()->lists('name', 'festival_id'));