【问题标题】:Check collection exist支票集合存在
【发布时间】:2020-05-26 14:01:32
【问题描述】:

我有这个代码:

@foreach($subCategories as $subCategory)
  <tr data-id="{{ $subCategory->id }}" id="{{ $subCategory->id }}" class="sortItem">
    <td class="text-left">
      {!! Str::limit($subCategory->category_name, 80, '...') !!}
      <p class="text-muted"></p>
    </td>
    <td class="text-center">
      @if ($subCategory->enable == 1)
        <span class="label font-weight-bold label-lg label-light-success label-inline">
          <i class="ft-thumbs-up"></i> aktywny
        </span>
      @else
        <span class="label font-weight-bold label-lg label-light-danger label-inline">
          <i class="ft-thumbs-down"></i> nieaktywny
        </span>
      @endif
    </td>
    <td class="text-center">
      <a href="{{ route('subcategory.index', ['parentId'=>$subCategory->id]) }}" class="badge badge-primary badge-md h-30px">
        <i class="ft-aperture"></i> Rozwiń
      </a>
      <a href="{{ route('subcategory.edit', ['id'=>$subCategory->id]) }}" class="badge badge-info badge-md h-30px">
        <i class="ft-aperture"></i> Edytuj
      </a>
      <form method="post" action="{{ route('subcategory.destroy', ['id'=>$subCategory->id, 'parentId'=>$parentId]) }}" class="d-inline-block" onSubmit="showAlert();{return false;}">
        @method('DELETE')
        {{ csrf_field()  }}
        <input type="submit" class="removeBtn" value="Usuń">
      </form>
    </td>
  </tr>
@endforeach

上面的代码有时候会出错:

试图获取非对象的属性“id”(查看: /var/www/resources/views/admin/categories/subcategory_list.blade.php)

当我调试时,问题是在我的 $subCategory 中有:

App\Models\Category {#487 ▼
  #quarded: array:1 [▶]
  #fillable: array:11 [▶]
  +timestamps: false
  #connection: "mysql"
  #table: "categories"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:12 [▶]
  #original: array:12 [▶]
  #changes: []
  #casts: []
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
  #pending: null
  #moved: false
}

当我在调试时:

Kalnoy\Nestedset\Collection {#487 ▼
  #items: array:3 [▼
    0 => App\Models\Category {#1423 ▶}
    1 => App\Models\Category {#1422 ▶}
    2 => App\Models\Category {#1426 ▶}
  ]
}

这段代码运行良好。

如何确保我的 foreach 函数不显示错误?

【问题讨论】:

  • 我猜最简单的就是查id@if (isset($model-&gt;id))
  • 请分享更多细节。通常,循环不应返回 NULL 的元素
  • 哪一行抛出该错误? $subCategory-&gt;id 调用都不会导致该错误。如果每个$subCategory 都是Category 模型的一个实例,那么它就是一个对象,-&gt;id 将返回idnull。您看到此错误的主要时间是尝试访问 array (['id']) 或 null-&gt;id['id'] 都会失败)上的属性(如 -&gt;id)时

标签: php laravel laravel-5 laravel-7


【解决方案1】:

您可以检查 $subCategories 变量的计数是否大于 0

@if($subCategories->count())
here goes your loop
@endif

laravel 集合实例有一个名为count 的方法。你可以用它检查集合的长度,只有当它至少有 1 个结果时才循环它。

如果$subCategories-&gt;count() 返回 0,它会将其解释为FALSE,并且不会输入下面的代码。只要$subCategories-&gt;count() 大于 0,它将为真并执行 if 语句中的代码。如果您需要更多解释,请随时询问。

【讨论】:

  • 如果计数元素返回零元素,则不进入循环
  • 是的,你不需要用@foreach() 包裹@if(count(...)),除非你需要做一些不同的事情,如果它是空的。即使这样,您也可以使用 @forelse(...) @empty ... @endforelse 代替:laravel.com/docs/7.x/blade#loops
猜你喜欢
  • 2011-10-28
  • 2011-02-23
  • 2017-01-09
  • 2021-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-03
  • 1970-01-01
相关资源
最近更新 更多