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

问题描述:

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;
    }


} 

我在下面有两个具有1对1关系的模型。 他们的工作就像一个魅力,但突然(可能因为数据库中的一些更新(添加了2个新列))它停止工作。 我只是在尝试获得付款时才会收到错误。 例如。 p>

  $ fare-> payment-> amount;  
  code>  pre> 
 
 

给出错误:尝试获取非对象的属性 当我使用DD()时; 调试我看到以下显示。 Pastebin p>

有人知道该怎么做或如何解决这个问题 ? p>

下面你可以找到模型 p>

  class Fare extends Eloquent {
 
 protected $ table ='fare'; \  n 
公共功能支付()
 {
返回$ this-> hasOne('Payment'); 
} 
 
公共功能电子邮件()
 {
返回$ this->电子邮件 ; 
} 
 
公共函数getTimeagoAttribute()
 {
 $ date = Carbon :: createFromTimeStamp(strtotime($ this-> created_at)) - > diffForHumans(); 
返回$ date; \  n} 
 
} 
 
 
class付款扩展Eloquent {
 
 protected $ table ='payment'; 
 
公共函数票价()
 {
返回$ this-> belongsTo(' 票价'); 
} 
 
公共函数状态()
 {
返回$ this-> belongsTo('Status'); 
} 
 
公共函数scopeApproved($ query)
  {
返回$ query-> where('status',1); 
} 
 
公共函数scopeDecline  d($ query)
 {
返回$ query-> where('status',2); 
} 
 
公共函数getTimeagoAttribute()
 {
 $ date = Carbon :: createFromTimeStamp(  strtotime($ this-> created_at)) - > diffForHumans(); 
返回$ date; 
} 
 
 
} 
  pre> 
  div>

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

The error is as given below:

Trying to get property of non-object

It rises when you access payment from Fare like this:

$fare->payment->amount;

According to your dd() result you have some Fare objects without related Payment:

// In your pastebin you have similar entries
["payment"]=>
NULL

So, in the returned collection you have some Fare objects without related Payment so this error is arising, make sure that the Fare object has a related Payment object before you try to access one. You may try something like this:

{{ $fare->payment ? $fare->payment->amount : '' }}