Php Laravel与自定义主键的一对多关系不起作用

Php Laravel与自定义主键的一对多关系不起作用,php,mysql,orm,laravel-4,eloquent,Php,Mysql,Orm,Laravel 4,Eloquent,我正在尝试在laravel中实现一对多关系 我的表具有自定义主键名称而不是id 我也设置了$primartKey属性,但关系似乎不起作用 activities |act_ID|act_acc_ID|......... categories |acc_ID|....... 这是我的模型 class Adventure extends \Eloquent { /** * @var string $table the name of the table */

我正在尝试在laravel中实现一对多关系 我的表具有自定义主键名称而不是id

我也设置了
$primartKey
属性,但关系似乎不起作用

activities
|act_ID|act_acc_ID|.........

categories
|acc_ID|.......
这是我的模型

class Adventure extends \Eloquent {


    /**
     * @var string $table the name of the table
     */
    protected $table = 'activities';

    /**
     * @var string $primaryKey the primary key of the table
     */
    protected $primaryKey = 'act_ID';

    /**
     * @var bool timestamps updated_at and created_at columns flag
     */
    public $timestamps = false;

    /**
     *
     */
    public function category(){
        $this->belongsTo('Category','act_acc_ID');
    }
}


class Category extends \Eloquent {

    /**
     * @var string $table the name of the table
     */
    protected $table = 'categories';

    /**
     * @var string $primaryKey the primary key of the table
     */
    protected $primaryKey = 'acc_ID';

    /**
     * @var bool timestamps updated_at and created_at columns flag
     */
    public $timestamps = false;

    public function adventures(){
        $this->hasMany('Adventure','act_acc_ID');
    }
}
现在,每当我尝试从“冒险”中访问类别或从我得到的类别中访问“冒险”时

关系方法必须返回类型为的对象 照亮\数据库\雄辩\关系\关系

我在这里做错了什么?? 有很多冒险的类别是15,所以我尝试
我尝试了
Categories::find(15)->adventures
也尝试了
Categories::find(15)->adventures()
您没有使用
return
关键字,它应该是这样的:

public function category(){
    return $this->belongsTo('Category','act_acc_ID');
}

public function adventures(){
    return $this->hasMany('Adventure','act_acc_ID');
}

在两种关系方法中添加
return
关键字。

您必须在类别模型中设置此项

public $incrementing = false;

是的,我相信,因为我们犯了愚蠢的错误(我自己也犯过),所以你应该休息一下,休息后需要用新的大脑重新开始。当出现问题并且找不到解决方案时,休息一下,这很有帮助:-)我从
CI
切换,有一天我浪费了3个小时来找出一个bug,结果是,我的重定向没有在屏幕上显示任何内容,但我在前几天找到了它,就是,我没有使用带有重定向的
return
关键字,它必须像
返回重定向(…)
而不是
重定向::(…)
。改变我在
CI
中使用的做法花了一些时间。祝你一切顺利:-)我也有同样的问题。这解决了问题,谢谢。