dousu1900 2014-03-16 20:46
浏览 45

关系方法必须返回一个类型为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.

  • 写回答

1条回答 默认 最新

  • dongluo8439 2014-03-16 22:20
    关注

    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
    ...
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作