私はいくつかの助けが必要です。Laravel - 作成とアタッチ - 多対多の関係
私はこれらのテーブルを持っています:ユーザ、購入、コーデック。私は、多対多の関係を持っている:買物、コーデック、
buy_codecテーブル
Schema::create('codecs', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
Schema::create('buys', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('name');
});
Schema::create('buy_codec', function (Blueprint $table) {
$table->increments('id');
$table->integer('buy_id')->unsigned();
$table->foreign('buy_id')->references('id')->on('buys')->onDelete('cascade');
$table->integer('codec_id')->unsigned();
$table->foreign('codec_id')->references('id')->on('codecs')->onDelete('cascade');
$table->timestamps();
});
モデル
class Codec extends Model
{
protected $guarded = ['id'];
public function buy() {
return $this->belongsToMany('App\Buy');
}
}
class Buy extends Model
{
protected $guarded = ['id'];
public function codec() {
return $this->belongsToMany('App\Codec');
}
}
class User extends Authenticatable
{
public function buy() {
return $this->hasMany('App\Buy');
}
}
は私が買うテーブルを移入したいですuser_idでコーデックを添付し、コーデックテーブル。
これはを購入してを作成するフォームです。
{!! Form::open(['method'=>'POST', 'action'=>['[email protected]', $usr->id]]) !!}
<div class="form-group">
{!! Form::label('name', 'Name:') !!}
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-font"></i></span>
{!! Form::text('name', null, ['class'=>'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('codecs', 'Outbound Codecs:') !!}
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-language"></i></span>
{!! Form::select('codecs[]', $codecs, null, ['class'=>'form-control', 'multiple'=>true]) !!}
</div>
</div>
{!! Form::submit('Submit', ['class'=>'btn btn-info']) !!}
{!! Form::close() !!}
コーデックを添付しないとすべて正常に動作します。
UserBuyController
がどのように私は買いレコードを作成することができます(私は買いIDを必要とするため)と比べて、ピボットテーブルのコーデックを添付?
ありがとうございます!
今ではこれらの作業です: – confm
http://pastebin.com/5usuutCS – confm
どのように私は今デ・モデルを結合できますか? – confm