Laravel - 创建和附加 - 多对多关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40151395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - Create and Attach - Many to Many relationship
提问by confm
I need some help.
我需要帮助。
I have these tables: users, buys and codecs. I have a many-to-many relationship: buys, codecs, buy_codec
我有这些表:用户、购买和编解码器。我有一个多对多的关系:buys,codecs,buy_codec
Tables
表
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();
});
Models
楷模
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');
}
}
I want to populate the buytable with the user_id, and to attach the codecs from the codecstable.
我想用 user_id填充购买表,并从编解码器表中附加编解码器。
This is the buycreate form.
这是购买创建表单。
{!! Form::open(['method'=>'POST', 'action'=>['UserBuyController@store', $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() !!}
Everything works fine, if i don't attach the codecs.
一切正常,如果我不附加编解码器。
UserBuyController
用户购买控制器
class UserBuyController extends Controller
{
public function create($userId)
{
$codecs = Codec::lists('name', 'id');
$usr = User::findOrFail($userId);
return view('buy.create', compact('usr', 'codecs'));
}
public function store($userId, Request $request)
{
$usr = User::findOrFail($userId)->buy()->create($request->all());
return view('buy.index');
}
}
How can I create the buy record(because i need the buy id) and than attach the codecs in the pivot table?
如何创建购买记录(因为我需要购买 ID)而不是在数据透视表中附加编解码器?
Thanks!
谢谢!
回答by Mas Hary
You can attach() or sync(). I always store pivot data with attach() function.
您可以附加() 或同步()。我总是使用 attach() 函数存储数据透视数据。
$buy = Buy::findOrFail($id);
$buyId = $request->input('codec');
$buy->codec()->attach($buyId);
回答by Anandhan
In model, You have to mention the intermediate table.
在模型中,您必须提到中间表。
class Codec extends Model
{
protected $guarded = ['id'];
public function buy() {
return $this->belongsToMany('App\Buy', 'buy_codec', 'codec_id', 'buy_id');
}
}
and
和
class Buy extends Model
{
protected $guarded = ['id'];
public function codec() {
return $this->belongsToMany('App\Codec', 'buy_codec', 'buy_id', 'codec_id');
}
}
And then in controller,
然后在控制器中,
public function store($userId, Request $request)
{
$buy = User::findOrFail($userId)->buy()->create($request->all());
$buy->codec()->attach([codec_ids]);
return view('buy.index');
}
You can attach more codec objects or ids using attach method.
您可以使用附加方法附加更多编解码器对象或 ID。
Please refer this link
请参考这个链接
https://laravel.com/docs/5.2/eloquent-relationships#inserting-related-models
https://laravel.com/docs/5.2/eloquent-relationships#inserting-related-models
回答by smartrahat
There are few ways you can achieve this. You can attach()
or sync()
. I always store pivot data with attach()
function.
有几种方法可以实现这一点。你可以attach()
或sync()
。我总是用attach()
函数存储数据透视数据。
$usr = User::findOrFail($userId)->create($request->all());
$usr->buy()->attach($request->codecs);
or, you can inline the query:
或者,您可以内联查询:
$usr = User::findOrFail($userId)->create($request->all())->buy()->attach($request->codecs);