У меня проблема во многих ко многим вставкам.Мой код в контроллере правильный.После того, как я добавляю два успешно данных, третий раз, когда я пытаюсь вставить, выдает ошибку:
Вызов функции-члена bagcollects () для null
иошибка в этом коде
$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',
];