【问题标题】:Trying to get property 'regular_price' of non-object in laravel 5.5试图在 laravel 5.5 中获取非对象的属性“regular_price”
【发布时间】:2018-04-04 20:33:51
【问题描述】:

我正在尝试从其他表中获取值,但出现此错误 我的模型是这样的:

class PageList extends Model
{
    protected $table = 'page_master';

    protected $fillable = ['business_id', 'page_url', 'page_name'];

    public function particulars()
    {
        return $this->hasOne('App\Sale','id');
    }
}

我的刀片模板是:

<p>{{$value->particulars->regular_price}}</p>

我想从 page_particulars 表中获取值,该值是 regular_price,但我在尝试获取非对象属性时遇到错误。 我在哪里做错了? 任何帮助将不胜感激!

            public function pageListHere()
{
    $list = PageList::all();
    return view('page-list',compact('list'));
}

【问题讨论】:

  • $value 是一个数组,而不是一个雄辩的模型(可能)
  • 所以我可以修改为数组,这样我的解决方案就可以解决了!
  • 你能上传dd($value)的输出吗?
  • 也是查询结果的部分
  • 它告诉我:object(App\PageList)#148 (26) { ["table":protected]=> string(11) "page_master" ["fillable":protected]=> 数组(3) { [0]=> string(11) "business_id" [1]=> string(8) "page_url" [2]=> string(9) "page_name" } ["connection":protected]=> string(5) "mysql" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(7) { ["id"]=> int(1)

标签: php laravel relational-database laravel-5.5


【解决方案1】:

使用optional()辅助方法

<p>{{optional($value->particulars)->regular_price}}</p>

【讨论】:

    【解决方案2】:

    compact('list') 不是 compact($list) 不会传递你期望的收集结果,而只是一个字符串。

    旁边 public function particulars() 似乎没有为此 $value 返回对象。所以结果可能是null

    【讨论】:

      猜你喜欢
      • 2018-06-06
      • 1970-01-01
      • 2018-03-03
      • 2018-07-03
      • 2018-02-10
      • 2019-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多