dynamic input field laravel-livewire
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Student;
use App\Http\Livewire\Field;
use Illuminate\Http\Request;
class Students extends Component
{
public $students, $first_name, $last_name, $student_id;
public $updateMode = false;
public $inputs = [];
public $i = 1;
public function add($i)
{
$i = $i + 1;
$this->i = $i;
array_push($this->inputs ,$i);
}
public function remove($i)
{
unset($this->inputs[$i]);
}
public function render()
{
$this->students = Student::all();
return view('livewire.students');
}
private function resetInputFields(){
$this->first_name = '';
$this->last_name = '';
}
public function store()
{
$validatedDate = $this->validate([
'first_name.0' => 'required',
'last_name.0' => 'required',
'first_name.*' => 'required',
'last_name.*' => 'required',
],
[
'first_name.0.required' => 'First name field is required',
'last_name.0.required' => 'Last name field is required',
'first_name.*.required' => 'First name field is required',
'last_name.*.required' => 'Last name field is required',
]
);
foreach ($this->first_name as $key => $value) {
Student::create(['first_name' => $this->first_name[$key], 'last_name' => $this->last_name[$key]]);
}
$this->inputs = [];
$this->resetInputFields();
session()->flash('message', 'Students Created Successfully.');
}
}
?>