What's new

PHP Laravel Error "Only arrays and Traversables can be unpacked"

Lee gee

Journeyman
Ano kayang Problema nito Guys, Error "Only arrays and Traversables can be unpacked" . pag ni remove ko yung event.detail[0] at gawing event.detail lang, nawawala yung error pero undefined naman yung value na nakukuha ko sa section_name at section_id at hindi rin na dedelete yung data.
You do not have permission to view the full content of this post. Log in or register now.

Livewire/Section


public function ConfirmDelete($section_id, $section_name) {
$this->dispatch('Swal:DeletedRecord', [
'section_name' => $section_name,
'title' => 'Are You Sure You Want to Delete? <span class="text-danger">' . $section_name . '</span>',
'id' => $section_id,
]);
}
protected $listeners = ['RecordDeleted' => 'DeletedSection'];
public function RecordDeleted($section_id) {
$sectiondelete = SectionModel::find($section_id);
$sectiondelete->delete();
}


table.blade.php:

<div>
<table class="table" width="100%">
<thead>
<tr>
<th>Section Name</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@forelse($sectionsdata as $section)
<tr>
<td>{{ $section->section_name }}</td>
<td>{{ $section->section_status === 1 ? 'Enabled' : 'Disabled' }}</td>
<td>
<div class="btn-group">
<a href="#editSection" data-toggle="modal" wire:click.prevent="editSection({{ $section->id }})" class="btn btn-info"><i class="fa fa-edit"></i></a>
<a href="#" wire:click.prevent="ConfirmDelete({{$section->id}}, '{{$section->section_name}}')" class="btn btn-danger"><i class="fa fa-trash"></i></a>
</div>
</td>
</tr>
@include('sections.edit')
empty
@endforelse

</tbody>
</table>
</div>
 
The error "Only arrays and Traversables can be unpacked" occurs when you try to access an element from an undefined or non-iterable variable.

In your code, the error is happening because the value of event.detail is not an array or a traversable object. The event.detail object seems to be an associative array, so you can directly access its properties using dot notation without unpacking it.

To fix the error, you need to update your JavaScript code and remove the [0] indexing from event.detail. Here's how you can modify your code:

JavaScript:
window.addEventListener('Swal:DeletedRecord', event => {
  Swal.fire({
    title: 'Are you sure you want to delete this section: ' + event.detail.section_name + '?',
    text: event.detail.title,
    icon: 'warning',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'Yes, delete it!'
  }).then((result) => {
    if (result.isConfirmed) {
      Livewire.dispatch('RecordDeleted', event.detail.id)
      Swal.fire(
        'Deleted!:',
        'Your file has been deleted.',
        'success'
      )
    }
  })
});

By removing the [0] indexing, you can access the properties directly from event.detail. Now, the section_name, title, and id should be properly passed to the ConfirmDelete method.

Additionally, to avoid the "undefined" value issue, make sure that the $sectionsdata variable in your controller contains the necessary data and is properly passed to the Blade view.
 
Back
Top