I can’t do the model binding inside custom method in livewire component cause the data return as a json
Code
Parent Component:
<?php
namespace App\Http\Livewire;
use App\Contact;
use Livewire\Component;
class Nesting extends Component
{
public $contacts;
public function mount()
{
$this->contacts = Contact::all();
}
public function removeContact(Contact $contact)
{
$contact->delete();
return $this->contacts;
}
public function render()
{
return view('livewire.nesting');
}
}
Child Component:
<?php
namespace App\Http\Livewire;
use App\Contact;
use Livewire\Component;
class NestingChild extends Component
{
public $contact;
public function mount(Contact $contact)
{
$this->contact = $contact;
}
public function render()
{
return view('livewire.nesting-child');
}
}
view
Parent Component:
<div>
@foreach($contacts as $contact)
<div>
@livewire('nesting-child', ['contact' => $contact], key($contact->id))
<button wire:click="removeContact('{{$contact->id}}')">Remove Contact</button>
</div>
@endforeach
</div>
Child Component:
<div>
{{$contact->name}}
</div>
Any Idea?