dongtui8593 2014-09-27 18:40
浏览 29
已采纳

雄辩的关系停止了工作,不再起作用了

I have this two models below that have a 1 on 1 relationship. They worked like a charm but suddenly ( maybe because some updates in the database ( added 2 new columns )) it stopped working. I am getting the error only when I try to reach a payment that is part of a fare. For example.

$fare->payment->amount; 

Gives an error: Trying to get property of non-object When I use a DD(); to debug I see the following show up. Pastebin

Does someone know what to do or how to solve this?

Below u can find the models

class Fare extends Eloquent {

    protected $table = 'fare';

    public function payment()
    {
        return $this->hasOne('Payment');
    }

    public function email()
    {
        return $this->email;
    }

    public function getTimeagoAttribute()
    {
        $date = Carbon::createFromTimeStamp(strtotime($this->created_at))->diffForHumans();
        return $date;
    }

} 

class Payment extends Eloquent {

    protected $table = 'payment';

    public function fare()
    {
        return $this->belongsTo('Fare');
    }

    public function status()
    {
        return $this->belongsTo('Status');
    }

    public function scopeApproved($query)
    {
        return $query->where('status', 1);
    }

    public function scopeDeclined($query)
    {
        return $query->where('status', 2);
    }

    public function getTimeagoAttribute()
    {
        $date = Carbon::createFromTimeStamp(strtotime($this->created_at))->diffForHumans();
        return $date;
    }


} 
  • 写回答

2条回答 默认 最新

  • duanbo5230 2014-09-27 19:20
    关注

    Since it worked, I think you added a "payment" column in your table, so with $fare->payment you access to the attribute not the relation, you have 3 choices:

    1. rename your column
    2. use $fare->payment()->first()->amount
    3. rename your relation
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向