douxunwei8259 2018-03-03 06:59
浏览 5

Laravel:关系问题?

I'm beginning to think why did Laravel implement relationships to their framework, they've never worked for me and their a huge stress to fix when they break. This is the 5th time my relationships are returning null, even when ensuring I've set them up properly?

class UserStats extends Authenticatable
{
    protected $table = 'habbo_user_stats';
    public $timestamps = false;
    protected $guarded = ['id'];

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

And

class User extends Authenticatable
{
    protected $table = 'habbo_users';
    public $timestamps = true;
    protected $guarded = ['id'];

    public function stats() {
        return $this->belongsTo(UserStats::class, 'user_id');
    }
}

although, when calling

{{ $user->stats->some_column }}

stats is returning null... $user isn't null.

  • 写回答

2条回答 默认 最新

  • dqmq0654 2018-03-03 07:06
    关注

    I think you have to define the owner of the relationship too. Ie:

    public function stats() {
       // $this->hasMany OR $this->hasOne, depending on your use case.
       return $this->hasMany(UserStats::class, 'user_id');
    

    }

    We need to know here, does the user have many userstats? or the userstats have many user records? what are you planning to do here?

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)