2016-10-20 36 views
1

我需要一些幫助。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)和比附加編解碼器中的數據透視表?

謝謝!

回答

2

在模型中,您必須提及中間表。

class Codec extends Model 
{ 
    protected $guarded = ['id']; 
    public function buy() { 
     return $this->belongsToMany('App\Buy', 'buy_codec', 'codec_id', 'buy_id'); 
    } 
} 

class Buy extends Model 
{ 
    protected $guarded = ['id']; 
    public function codec() { 
     return $this->belongsToMany('App\Codec', 'buy_codec', 'buy_id', 'codec_id'); 
    } 
} 

然後在控制器,

public function store($userId, Request $request) 
{ 
     $buy = User::findOrFail($userId)->buy()->create($request->all()); 
     $buy->codec()->attach([codec_ids]); 
     return view('buy.index'); 
} 

可以使用附加方法附加更多的編解碼器對象或IDS。

請參考以下鏈接

https://laravel.com/docs/5.2/eloquent-relationships#inserting-related-models

+0

現在,它正在與這些: – confm

+0

http://pastebin.com/5usuutCS – confm

+0

我該怎麼辦德模型現在結合? – confm

1

有您能做到這一點幾種方法。您可以attach()sync()。我總是使用attach()函數存儲數據透視數據。

$usr = User::findOrFail($userId)->create($request->all()); 
$usr->buy()->attach($request->codecs); 

或者,您現在可以查詢:

$usr = User::findOrFail($userId)->create($request->all())->buy()->attach($request->codecs); 
1

您可以將()或者同步()。我總是使用attach()函數存儲數據透視數據。

$buy = Buy::findOrFail($id); 
    $buyId = $request->input('codec'); 
    $buy->codec()->attach($buyId);