Many to many insertion error Call to a member function Laravel 5.5
Many to many insertion error Call to a member function Laravel 5.5
I have a trouble in many to many insertion. My code in controller is right. After I add the two data which is successful, the third time I try to insert produce an error:
Call to a member function bagcollects() on null
and the error is on this code
$collection->bagcollects()->attach($bagcollect->id);
I just don't understand why this error occurred!
I'll show the whole code of CollectionsController store method:
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]);
Migration on collections
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');
);
Migration 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();
);
Migration 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();
);
collections show.blade.php add modal
<!-- 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 model
protected $fillable = [
'id',
'assignment_id',
'bag_id'
];
public $timestamps = false;
public function assignment()
return $this->belongsTo('AppAssignment');
public function bagcollects()
return $this->belongsToMany('AppBagcollect');
Bagcollect.php
protected $fillable = [
'bag_id',
'weight'
];
public function collections()
return $this->belongsToMany('AppCollection');
public function bag()
return $this->belongsTo('AppBag');
BagcollectCollection.php
protected $table = "bagcollect_collection";
protected $fillable = [
'bagcollect_id',
'collection_id',
];
Collection
is already a class in Laravel; you might run into issues with your Collection
model vs the Collection
helper (laravel.com/docs/5.6/collections). Consider changing your table/model names.– Tim Lewis
Sep 6 '18 at 14:21
Collection
Collection
Collection
@TimLewis she probably use with its own namespace, shouldn't be a problem..
– Teoman Tıngır
Sep 6 '18 at 14:22
@HasanTıngır Yes, probably, but without seeing the
use
statement, it's hard to say. Changed my wording on the comment it make it more of a warning.– Tim Lewis
Sep 6 '18 at 14:24
use
can you dump
$collection
var with dd() method ?– Teoman Tıngır
Sep 6 '18 at 14:29
$collection
1 Answer
1
Call to a member function ... on null
null
Means that you're trying to call a function on a null
object. Review your code:
null
$collection = Collection::find($request->input('collection_id'));
With your current logic, $collection
could be null
, so calling
$collection
null
$collection->bagcollects() ...
Will throw this error.
Debug the result of Collection::find(...)
and ensure that $collection
is not null
.
Collection::find(...)
$collection
null
I try to use dd in my controller code. When I enter the data, the collection_id is passed from the show.blade.php, but on the second try, the collection_id became null. I don't know because I think it should be passed knowing the it is in the show and the collection_id is existing on that view
– Maria Theresa Chavez
Sep 8 '18 at 6:09
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
can you share relation model ?
– Teoman Tıngır
Sep 6 '18 at 14:20