我在多对多插入中遇到麻烦。我在控制器中的代码是正确的。添加成功的两个数据后,第三次尝试插入会产生错误:
以null调用成员函数bagcollects()
并且错误在此代码上
$collection->bagcollects()->attach($bagcollect->id);
我只是不明白为什么会发生此错误!
我将展示CollectionsController存储方法的整个代码:
public function addbag(Request $request){
$collection = Collection::find($request->input('collection_id'));
$bagcollect = Bagcollect::create([
'bag_id' => $request->input('bag_id'),
'weight' => $request->input('weight')
]);
$collection->bagcollects()->attach($bagcollect->id);
return redirect()->route('collections.show', ['collection'=> $collection->id]);
}
迁移收藏集
Schema::disableForeignKeyConstraints();
if(!Schema::hasTable('collections')){
Schema::create('collections', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->increments('id');
$table->integer('assignment_id')->unsigned();
$table->foreign('assignment_id')->references('id')->on('assignments')->onUpdate('cascade')->onDelete('cascade');
$table->timestamp('collected_on');
});
}
迁移bagcollects
Schema::create('bagcollects', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->increments('id');
$table->integer('bag_id')->unsigned();
$table->double('weight', 8, 2);
$table->foreign('bag_id')->references('id')->on('bags');
$table->timestamps();
});
迁移bagcollect_collection
Schema::create('bagcollect_collection', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->increments('id');
$table->integer('bagcollect_id')->unsigned();
$table->integer('collection_id')->unsigned();
$table->foreign('bagcollect_id')->references('id')->on('bagcollects');
$table->foreign('collection_id')->references('id')->on('collections');
$table->timestamps();
});
集合show.blade.php添加模式
<!-- ADD MODAL -->
<div class="modal fade" role="dialog" id="addModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">New Collection</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-12">
<form class="form-horizontal" method="POST" action="{{ route('collections.addbag') }}">
{{ csrf_field() }}
<div class="row form-group">
<input class="form-control" name = "collection_id" id="collection_id" value="{{$collection->id}}" type="hidden">
<div class="{{ $errors->has('bag') ? ' has-error' : '' }}">
<div class="col-md-8">
<label for="bag_id">Bag</label>
<select class="form-control" required id="bag" name="bag_id">
<option value="" data-hidden="true"
selected="selected">
</option>
@foreach($bags as $bag)
<option value= "{{ $bag->id }}">
{{ $bag->name }}
</option>
@endforeach
</select>
</div>
</div>
<div class="{{ $errors->has('weight') ? ' has-error' : '' }}">
<div class="col-md-8">
<label for="weight">Weight</label>
<input type="text" class="form-control" id="weight" name= "weight" required>
</div>
</div>
</div>
<!-- SUBMIT BUTTON -->
<button type="submit" class="btn btn-success btn-fill pull-right" id="form-button-add">
Create
</button>
<button data-dismiss="modal" aria-hidden="true" class="btn btn-basic pull-right" style="margin-right: 2%">
Cancel
</button>
<div class="clearfix"></div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
Collection.php模型
protected $fillable = [
'id',
'assignment_id',
'bag_id'
];
public $timestamps = false;
public function assignment()
{
return $this->belongsTo('App\Assignment');
}
public function bagcollects()
{
return $this->belongsToMany('App\Bagcollect');
}
Bagcollect.php
protected $fillable = [
'bag_id',
'weight'
];
public function collections()
{
return $this->belongsToMany('App\Collection');
}
public function bag()
{
return $this->belongsTo('App\Bag');
}
BagcollectCollection.php
protected $table = "bagcollect_collection";
protected $fillable = [
'bagcollect_id',
'collection_id',
];
答案 0 :(得分:3)
在
null
上调用成员函数...
表示您正在尝试在null
对象上调用函数。查看您的代码:
$collection = Collection::find($request->input('collection_id'));
根据您当前的逻辑,$collection
可能是null
,因此调用
$collection->bagcollects() ...
将抛出此错误。
调试Collection::find(...)
的结果,并确保$collection
不是null
。