Laravel获取非对象的属性 [英] Laravel get property of non object

查看:73
本文介绍了Laravel获取非对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试确定某个对象是否为空时,它会告诉我:

When I try to determine if an object is empty it's telling me:

试图获取非对象的属性

Trying to get property of non-object

我正在这样做:

 $lastTicket = Auth::user()->ticket->last()->ticketid;
    if($lastTicket->isEmpty())
    {
        $lastTicket = 0;
    }

显然是Auth :: user()-> ticket-> last();还没有记录.我应该怎么做?我正在与Laravel合作.

Obviously Auth::user()->ticket->last(); isn't a record yet. How should I do this? I'm working with Laravel.

推荐答案

在获取属性之前,您需要检查集合是否为空:

You need to check that collection not empty before get the property:

    if(Auth::user()->ticket->last()->isEmpty())
    {
       $lastTicket = 0;
    }
    else
    {
        $lastTicket = Auth::user()->ticket->last()->lastId;
    }

简而言之:

$lastTicket = !Auth::user()->ticket->last()->isEmpty() ? $lastTicket = Auth::user()->ticket->last()->lastId : 0;

这篇关于Laravel获取非对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆