【问题标题】:Laravel Blade Displaying DataLaravel Blade 显示数据
【发布时间】:2021-03-08 06:03:46
【问题描述】:

我对 laravel (version 7) {{ }} 的语法有一个奇怪的问题,我使用这样的 PHP 语法:

<?php echo $thread->lastReply->user->name; ?>

我让我的用户显示,但使用刀片语法:

{{$thread->lastReply->user->name}}

我收到了这个错误:

错误异常
试图获取非对象的属性“用户”

问题可能出在哪里?

编辑: 线程中的lastReplay方法是这样的:

         public function getLastReplyAttribute()
            {
                $lastReplay = $this->replies->last();
                dd($lastReplay);
                return $lastReplay;
            }

和转储:

                \Models\Reply {#3012 ▼
              #table: "ww_replies"
              #connection: "mysql"
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:10 [▼
                "id" => "42"
                "thread_id" => "45"
                "user_id" => "1"
                "title" => null
                "body" => "Quia minima velit illo aut vitae eum et. Fuga consequatur atque earum fuga ad aut molestiae. Expedita ratione perspiciatis laborum hic quis unde. Numquam ipsam  ▶"
                "slug" => null
                "ip" => null
                "created_at" => "2020-11-23 12:24:19"
                "updated_at" => "2020-11-23 12:24:19"
                "deleted_at" => null
              ]
              #original: array:10 [▶]
              #changes: []
              #casts: []
              #classCastCache: []
              #dates: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: []
              #touches: []
              +timestamps: true
              #hidden: []
              #visible: []
              #fillable: []
              #guarded: array:1 [▶]
            }

【问题讨论】:

  • 您是否将$thread 作为数据传递给刀片视图?
  • 我会使用{{ dump($thread-&gt;lastReply) }} 看看那里发生了什么。我假设您处于循环中,并且您的迭代之一没有显示
  • 是的,它在循环内

标签: php laravel laravel-blade


【解决方案1】:

这意味着$thread-&gt;lastReply 不是一个对象,或者通常它是null。处理你可以使用条件示例:

{{$thread->lastReply ? $thread->lastReply->user->name : 'no last reply user'}}

{{$thread->lastReply->user->name ?? 'no last reply user'}}

【讨论】:

    【解决方案2】:

    你的问题来自-&gt;lastReply。也许有些用户没有lastReply

    尝试以下方法:

       {{  optional($thread->lastReply, function ($lastReply)
                {
                    return $lastReply->user->name;
                });
        }}
    

    【讨论】:

      【解决方案3】:

      您的查询返回的是数组还是对象?如果你把它转储出来,你可能会发现它是一个数组,你所需要的只是一个数组访问([])而不是对象访问(->)。

      【讨论】:

        【解决方案4】:
        ErrorException
        Trying to get property 'user' of non-object
        

        这个错误意味着 lastReply 返回 null 或其数组

        {{$thread->lastReply->user->name}}
        

        在这种语法中,您需要在添加关系之前检查一些数据

        1. 如果 lastReply 返回数据则转储

          {{dd($thread-&gt;lastReply)}}

        2. 用户应该存在于模型中并且在关系中具有正确的外键。另外它不应该为空,您也可以通过以下代码检查

          {{dd($thread-&gt;lastReply-&gt;user)}}

        如果两个转储都返回数据,则表示 lastReply 或用户正在返回数组。所以需要使用数组[]语法来获取数据。 也可以使用{!! !!}打印数据转义内容

        【讨论】:

          猜你喜欢
          • 2018-02-04
          • 2016-10-02
          • 1970-01-01
          • 2021-09-04
          • 2018-06-04
          • 1970-01-01
          • 2018-04-25
          • 2022-12-10
          • 2017-01-05
          相关资源
          最近更新 更多