The code provided is missing the logic to differentiate between creating a new team and editing an existing team. Currently, both the Create and Edit components have the same logic for saving the team data, which is causing the issue where the data is always saved as a new team.
To fix this issue, you need to modify the submit method in the Edit component to update the existing team instead of creating a new one. You can achieve this by using the update method instead of the save method.
Here's the updated code for the Edit component:
<?php
namespace App\Http\Livewire\Team;
use App\Models\Team;
use App\Models\User;
use Livewire\Component;
class Edit extends Component
{
public Team $team;
public array $listsForFields = [];
public function mount(Team $team)
{
$this->team = $team;
$this->initListsForFields();
}
public function render()
{
return view('livewire.team.edit');
}
public function submit()
{
$this->validate();
$this->team->update();
return redirect()->route('admin.teams.index');
}
protected function rules(): array
{
return [
'team.name' => [
'string',
'required',
],
'team.owner_id' => [
'integer',
'exists:users,id',
'required',
],
];
}
protected function initListsForFields(): void
{
$this->listsForFields['owner'] = User::pluck('name', 'id')->toArray();
}
}
With this change, the Edit component will now update the existing team instead of creating a new one when the form is submitted.