Relationship方法必须返回一个Relation (LogicException)类型的对象


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

我用过不少Laravel 4,这是我第一次遇到这个问题。

My page 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');
    }
}

可以看到分页有许多分页项,下面是属于分页的分页项模型。

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

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

}

如果我尝试像这样插入新模型:

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

我收到:

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

我还没有发现任何会导致这种错误的问题,任何帮助都是感激的,谢谢。

在"属于"关系中,您应该尝试传递要保存的对象而不是id。

$pager = pager::find(10);
$test->pager()->associate($pager);

顺便说一句,试着把类命名为大写…像

class Pager extends Eloquent
...