mardi 26 mai 2020

Check collection exist

I have this code:

@foreach($subCategories as $subCategory)
                                <tr data-id="" 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=""
                                           class="badge badge-primary badge-md h-30px"><i
                                                class="ft-aperture"></i> Rozwiń</a>

                                        <a href=""
                                           class="badge badge-info badge-md h-30px"><i
                                                class="ft-aperture"></i> Edytuj</a>
                                        <form method="post"
                                              action=""
                                              class="d-inline-block" onSubmit="showAlert();{return false;}">
                                            @method('DELETE')
                                            
                                            <input type="submit" class="removeBtn"
                                                   value="Usuń">
                                        </form>
                                    </td>
                                </tr>
                        @endforeach

Sometimes there is an error in the above code:

Trying to get property 'id' of non-object (View: /var/www/resources/views/admin/categories/subcategory_list.blade.php)

When I debug, problem is when in my $subCategory I have:

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
}

When I have in debug:

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

This code work fine.

How can I secure my foreach function so that it doesn't display an error?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3c5j4WL
via IFTTT

Aucun commentaire:

Enregistrer un commentaire