Hello partners. Let's see if you can give me a hand.
I've been dealing with this issue for 3 days now and I can't find a solution.
It turns out that I have a crud section.
These sections are created well, edited well, and deleted well. The problem comes when I edit a section, for example the first one, you delete another section. You go back to edit another one. and you delete, it fails here. Mysteriously the model disappears.
throwing an error "Livewire\Features\SupportLegacyModels\EloquentModelSynth::setDataOnModel(): Argument #1 ($model) must be of type Illuminate\Database\Eloquent\Model"
I have legancy_model_bending enabled.
I leave you the source code, I hope you find a solution.
Sorry, it especially happens when you edit the first row, save, delete and later delete the 2nd row. error occurs
@foreach ($course->sections as $item)
<article class="card mb-6" x-data="{open: true}" wire:key="sections-{{ $item->id }}">
<div class="card-body bg-gray-100">
@if ($section->id == $item->id)
<form wire:submit="update" class="flex justify-between items-center">
<input wire:model.live="sectionEdit.name" class="form-input w-full" placeholder="Escribe el nombre de la sección">
<div>
<i wire:click="cancel" class="fa fa-ban cursor-pointer text-blue-500 ml-2"></i>
</div>
@error('sectionEdit.name')
<span class="text-xs text-red-500">{{$message}}</span>
@enderror
</form>
@else
<header class="flex justify-between items-center">
<h1 x-on:click="open = !open" class="cursor-pointer">
<strong>Sección:</strong> {{ $item->name }}
</h1>
<div>
<i class="fas fa-edit cursor-pointer text-blue-500 mr-2" wire:click="edit({{ $item->id }})"></i>
<i class="fas fa-trash cursor-pointer text-red-500" wire:click="deleteSection({{ $item->id }})"></i>
</div>
</header>
<div x-show="open">
</div>
@endif
</div>
</article>
@endforeach
<div x-data="{ open: @entangle('isOpenSection') }">
<a x-show="!open" x-on:click="open = true" class="flex items-center cursor-pointer">
<i class="far fa-plus-square text-2xl text-red-500 mr-2"></i>
Agregar nueva sección
</a>
<article class="card" x-show="open">
<div class="card-body bg-gray-100">
<h1 class="text-xl font-bold mb-4">Agregar nueva sección</h1>
<div class="mb-4">
<input wire:model.live="sectionCreate.name" class="form-input w-full" placeholder="Escribe el nombre de la sección">
@error('sectionCreate.name')
<span class="text-xs text-red-500">{{$message}}</span>
@enderror
</div>
<div class="flex justify-end">
<button class="btn btn-danger" x-on:click="open = false">Cancelar</button>
<button class="btn btn-primary ml-2" wire:click="store">Agregar</button>
</div>
</div>
</article>
</div>
Component
public $course;
public $section;
public $flashVisible = true;
public $isOpenSection = false;
public SectionsCreateForm $sectionCreate;
public SectionsEditForm $sectionEdit;
public function mount(Course $course){
$this->course = $course;
$this->sectionCreate->course = $course;
$this->section = new Section();
}
public function render()
{
return view('livewire.instructor.courses-curriculum')
->layout('layouts.instructor', ['course' => $this->course]);
}
public function cancel(){
$this->section = new Section();
}
public function edit($sectionId){
$section = Section::find($sectionId);
$this->resetValidation();
$this->section = $section;
$this->sectionEdit->edit($section);
}
public function update(){
$this->sectionEdit->update();
$this->section = new Section();
$this->course = Course::find($this->course->id);
}
public function store(){
$this->sectionCreate->save();
$this->course = Course::find($this->course->id);
// Cerramos el panel
$this->isOpenSection = false;
}
public function destroySection($sectionId){
$section = Section::find($sectionId);
$section->delete();
$this->course = Course::find($this->course->id);
}
class SectionsCreateForm
class SectionsCreateForm extends Form
{
#[Rule('required')]
public $name;
public $course;
public function save(){
$this->validate();
$section = Section::create([
'name' => $this->name,
'course_id' => $this->course->id
]);
$this->reset(
'name'
);
}
}
class SectionsEditForm
class SectionsEditForm extends Form
{
#[Rule('required')]
public $name;
public $section;
public function edit($section){
$this->section = $section;
$this->name = $section->name;
}
public function update(){
$this->validate();
$this->section->update(
$this->only('name')
);
$this->reset(
'name',
);
}
}