Save a bunch of nested component
Hi, i'm learning livewire so maybe my question is is just a beginner.
I have a Model called Player with Id, name, surname, birthday and it's stored on db in "players" table. I would like to save in another table games played and the list of participants.
So "matches" will be a table with id, date, description, "matches-player" will be id, match_id, player_id, player_number_shirt.
I create a Component call MatchPlayerHeader and another one call MatchPlayerDetail
<?php
namespace App\Http\Livewire;
use Livewire\Component;
class MatchPlayerHeader extends Component
{
public function render()
{
return view('livewire.test');
}
public function saveEntries()
{
}
}
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Player;
class MatchPlayerDetail extends Component
{
public $shirtNumber;
public Player $player;
public $players;
protected $rules = [
'shirtNumber' => 'numeric',
'player.id' => 'required',
'player.surname' => 'required',
'player.name' => 'required',
'player.birthdate' => 'required'
];
public function mount()
{
$this->players= Player::get();
$this->player = Player::whereId(1)->first();
}
public function updatedPlayer($player)
{
$this->player= Player::whereId($player)->first();
}
public function render()
{
return view('components.details',['players'=>Player::get()]);
}
}
This is my component
<div>
<input wire:model="shirtNumber" type="text" />
<select wire:model="player.id">
@foreach ($players as $playerSelect)
<option value="{{$playerSelect->id}}" wire:key="{{ $playerSelect->id }}">{{$playerSelect->surname}} </option>
@endforeach
</select>
<input wire:model="player.surname" type="text" />
<input wire:model="player.name" type="text" />
<input wire:model="player.birthdate" type="text" />
</div>
and this is blade form
<form wire:submit.prevent="saveEntries">
@for ($i = 0; $i <20; $i++)
@livewire('details-component', ['shirtNum' => $i], key($i))
@endfor
<button type="submit">Save entries</button>
</form>
I don't understand how to get all 20 components and save into DB!
Maybe i have to create a new model ex. "Doppia" like this?
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Player;
class Doppia extends Model
{
use HasFactory;
public $shirtnumber;
public Player $player;
}
but then how can i bind it?
Please or to participate in this conversation.