Laravel-mongodb: Giving error Call to a member function modelKeys() on a non-object

Created on 15 Sep 2014  路  3Comments  路  Source: jenssegers/laravel-mongodb

Hey guys,

I m using jenssegers/Laravel-MongoDB.

I had two Model class eg: Category, Offering

class Category extends \Moloquent{
protected $collection = "categories";

public function offerings(){
return $this->belongsToMany('Offering', null, 'categories', 'offerings');
}

}

class Offering extends \Moloquent{
protected $collection = "offerings";
public function categories(){
return $this->belongsToMany('Category', null, 'categories', 'offerings');
}

}

Controller : CategoriesController

class CategoriesController extends \BaseController {

//save offerings for category
public function store(){
$category = Category::find(1);
$category->offerings()->sync(array(1,2,3));
$category->save();

$category = Category::find(2);
$category->offerings()->sync(array(1,2,3));
$category->save();
}
}

showing error
Call to a member function modelKeys() on a non-object

// First we need to attach any of the associated models that are not currently
// in this joining table. We'll spin through the given IDs, checking to see
// if they exist in the array of current ones, and if not we will insert.
$current = $this->parent->{$this->otherKey} ?: array();

// See issue #256.
if ($current instanceof Collection) $current = $ids->modelKeys();

$records = $this->formatSyncList($ids);

Wht should i do

Most helpful comment

I have encountered the same problem. I solved the problem if I change the following code:

BelongsToMany.php Line 115

if ($current instanceof Collection) $current = $ids->modelKeys();

to

if ($current instanceof Collection) $current = $current->modelKeys();

I'm wondering is it a typo? Because it should convert $cuurent to array instead of $ids, which has already converted to array in:

BelongsToMany.php Line 107

if ($ids instanceof Collection) $ids = $ids->modelKeys();

@jenssegers Please verify if this is a bug, thanks

All 3 comments

I have encountered the same problem. I solved the problem if I change the following code:

BelongsToMany.php Line 115

if ($current instanceof Collection) $current = $ids->modelKeys();

to

if ($current instanceof Collection) $current = $current->modelKeys();

I'm wondering is it a typo? Because it should convert $cuurent to array instead of $ids, which has already converted to array in:

BelongsToMany.php Line 107

if ($ids instanceof Collection) $ids = $ids->modelKeys();

@jenssegers Please verify if this is a bug, thanks

Is it a bug ? I wonder why is it still not fixed :)

I still experience this.

I tried pass an array of document id and Eloquent collection but neither is works. I tried apply a changes in @louisyau comment above and it's works.

Was this page helpful?
0 / 5 - 0 ratings