我需要一些帮助。
我有这些表:用户,购买和编解码器。我有多对多的关系:购买,编解码器, 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'=>['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() !!}
如果我不附加编解码器,一切正常。
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');
}
}
如何创建购买记录(因为我需要购买ID),而不是在数据透视表中附加编解码器?
谢谢!
答案 0 :(得分:3)
您可以附加()或同步()。我总是使用attach()函数存储数据透视数据。
$buy = Buy::findOrFail($id);
$buyId = $request->input('codec');
$buy->codec()->attach($buyId);
答案 1 :(得分: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');
}
您可以使用attach方法附加更多编解码器对象或ID。
请参阅此链接
https://laravel.com/docs/5.2/eloquent-relationships#inserting-related-models
答案 2 :(得分: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);