【问题标题】:"Undefined property: Illuminate\View\Engines\CompilerEngine::$“未定义的属性:Illuminate\View\Engines\CompilerEngine::$
【发布时间】:2019-11-10 16:31:26
【问题描述】:

我收到这个错误

Undefined property: Illuminate\View\Engines\CompilerEngine::${"id":1,"title":"abddad","description":"abddad.","created_at":"2019-06-27 22:25:18","updated_at":"2019-06-27 22:25:18"} (View: /home/vagrant/projectos/projectofinal/ProjectoFinal/resources/views/index.blade.php)

代码如下:

@foreach (\App\Award::all()->take(3) as $award)
  <div class="row">
    <div class="col-lg-12"> 
      <ul class="timeline">
          @if (count($this->$award) === 2)
          <li>
          <div class="timeline-image">
            <img class="rounded-circle img-fluid" src="img/awards/medal.png">
          </div>
          @else 
          <li class="timeline-inverted">
          <div class="timeline-image">
            <img class="rounded-circle img-fluid" src="img/awards/medal.png">
          </div>
          @endif
          <div class="timeline-panel">            
            <div class="timeline-heading">
              <h4 class="subheading">{{ $award->title }}</h4>
            </div>
            <div class="timeline-body">
              <p class="text-muted">{{ $award->description }}</p>
            </div>          
          </div>
        </li>
      </ul>       
    </div>
  </div>
  @endforeach

如果我使用 $award 而不是 $this->award

我有错误:

count(): Parameter must be an array or an object that implements Countable (View: /home/vagrant/projectos/projectofinal/ProjectoFinal/resources/views/index.blade.php)

在第 5 行 "@if (count($this->$award) === 2)" 中获取错误异常

我应该做些什么不同的事情?

希望这会有所帮助 dd($award):

  Award {#692 ▼
  #fillable: array:2 [▶]
  #connection: "mysql"
  #table: "awards"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
 #perPage: 15
 +exists: true
 +wasRecentlyCreated: false
 #attributes: array:5 [▶]
 #original: array:5 [▶]
 #changes: []
 #casts: []
 #dates: []
 #dateFormat: null
 #appends: []
 #dispatchesEvents: []
 #observables: []
 #relations: []
 #touches: []
 +timestamps: true
 #hidden: []
 #visible: []
 #guarded: array:1 [▶]
}

【问题讨论】:

  • 请用完整的错误更新您的问题
  • 这是第 5 行,您遇到的错误是什么?
  • 应该只是$award 而不是$this-&gt;award
  • 你能dd$award 告诉我们你得到了什么吗?
  • 你试过这个@if (count($award) === 2)

标签: php html vue.js


【解决方案1】:

以下代码通过变量 $award 的值调用 $this 的属性 ($this->$award) 表示如果 $award 确实包含 $this 的字符串匹配属性,您应该获取该属性的值。

相反,$award 包含一些看起来像字符串化的 json 对象的东西

{"id":1,"title":"abddad","description":"abddad.","created_at":"2019-06-27 22:25:18","updated_at":"2019-06-27 22:25:18"}

但是 $this 不包含这样的变量。

在第 5 行尝试以下代码,应该可以工作

@if (count($award) === 2)

【讨论】:

    【解决方案2】:

    我是这样解决的

    @if (($award->id) % 2 == "0") 
    

    感谢您的帮助!

    【讨论】:

    • 那是不正确的。如果你得到 id(3,4,5,6... 等等)怎么办。 ?
    猜你喜欢
    • 1970-01-01
    • 2019-07-11
    • 2016-11-23
    • 2021-01-12
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 2016-09-05
    • 2021-09-15
    相关资源
    最近更新 更多