Moe
Moe

Reputation: 470

Relationship method must return an object of type Relation (LogicException) Laravel 4.1

I have used Laravel 4 fair bit and it's the first time I've came across this problem.

My pager table:

class pager extends Eloquent
{

    protected $table = 'pagers';

    public function user()
    {
      return $this->belongsTo('User', 'bid');
    }


    public function pager_items()
    {
      return $this->hasMany('pager_item', 'pid');
    }

}

As you can see the pager has many pager items, below is the pager item model which belongs to pager.

class pager_item extends Eloquent
 {
    protected $table = 'pager_items';


    public function pager()
    {
     return $this->belongsTo('pager', 'pid');
    }


}

If I try to insert new model like so:

    $test = new pager_item;
    $test->description = 'test';
    $test->bid =1;
    $test->cid =1;
    $test->pid =1;
    $test->save();

I receive:

 LogicException
 Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation 

I haven't been able to spot any issues that will cause such error, any help is appreciated, thank you.

Upvotes: 0

Views: 2599

Answers (1)

angoru
angoru

Reputation: 5949

in a "belongs to" relation you should try to pass the object to save instead of the id.

$pager = pager::find(10);

$test->pager()->associate($pager);

btw, try to name the classes Uppercase... like

class Pager extends Eloquent
...

Upvotes: 1

Related Questions