FireBlade's avatar

Delete Model Livewire Component

I have a Livewire component that deletes models :

...

    
    public function setRecord($model,$recordId){
        $this->resetDialog();
        $this->model = $model;
        $this->recordId = $recordId;
    }
...
setRecord is called from Blade template :

```php
@if($canDelete)    
      <x-jet-dropdown-link @click="Livewire.emit('setDeleteRecord','Post',{{$row->id}});">
              {{ __('Delete') }}
      </x-jet-dropdown-link>
      <div class="border-t border-gray-100"></div>
@endif

How can I simplify pass the model from the Blade template without the manual model type "Post" and still delete the model ? Remember I plan to use the same Livewire component with different models eg Item, Location etc ?

0 likes
7 replies
FireBlade's avatar

This is how I delete the model:

if ($this->model == 'Post')Post::find($this->recordId)->delete(); elseif 
Snapey's avatar

ok if you want to allow the user to delete ANY record from ANY model !

FireBlade's avatar

Yes. There's an authorization check so this is not the issue...

FireBlade's avatar

When I write the blade view like this :

@foreach($posts as $post)  
        @if($canDelete)    
              <x-jet-dropdown-link rel="nofollow" href="#!"
                      @click="Livewire.emit('setDeleteRecord',{{$post}});">
                                 {{ __('Delete') }}
              </x-jet-dropdown-link>
              <div class="border-t border-gray-100"></div>
         @endif 
@endforeach

The Livewire component does not work:

if($this->model instanceof App\Models\Post) $this->model->delete();

I get an array instead of object.

FireBlade's avatar

livewire/livewire v2.12.1 A front-end framework ...

FireBlade's avatar

When i try it like this I also get array:

@foreach($posts as $post)  
        @if($canDelete)    
              <x-jet-dropdown-link rel="nofollow" href="#!"
                      @click="Livewire.emit('setDeleteRecord',{{App\Models\Post::find($post->id)}});">
                                 {{ __('Delete') }}
              </x-jet-dropdown-link>
              <div class="border-t border-gray-100"></div>
         @endif 
@endforeach

Please or to participate in this conversation.