dynamic input field laravel livewire

Solutions on MaxInterview for dynamic input field laravel livewire by the best coders in the world

showing results for - "dynamic input field laravel livewire"
Erica
21 Nov 2020
1<?php
2 
3namespace App\Http\Livewire;
4 
5use Livewire\Component;
6use App\Student;
7use App\Http\Livewire\Field;
8use Illuminate\Http\Request;
9 
10class Students extends Component
11{
12    public $students, $first_name, $last_name, $student_id;
13    public $updateMode = false;
14    public $inputs = [];
15    public $i = 1;
16 
17    public function add($i)
18    {
19        $i = $i + 1;
20        $this->i = $i;
21        array_push($this->inputs ,$i);
22    }
23 
24    public function remove($i)
25    {
26        unset($this->inputs[$i]);
27    }
28 
29    public function render()
30    {
31        $this->students = Student::all();
32        return view('livewire.students');
33    }
34 
35    private function resetInputFields(){
36        $this->first_name = '';
37        $this->last_name = '';
38    }
39 
40    public function store()
41    {
42        $validatedDate = $this->validate([
43                'first_name.0' => 'required',
44                'last_name.0' => 'required',
45                'first_name.*' => 'required',
46                'last_name.*' => 'required',
47            ],
48            [
49                'first_name.0.required' => 'First name field is required',
50                'last_name.0.required' => 'Last name field is required',
51                'first_name.*.required' => 'First name field is required',
52                'last_name.*.required' => 'Last name field is required',
53            ]
54        );
55 
56        foreach ($this->first_name as $key => $value) {
57            Student::create(['first_name' => $this->first_name[$key], 'last_name' => $this->last_name[$key]]);
58        }
59 
60        $this->inputs = [];
61 
62        $this->resetInputFields();
63 
64        session()->flash('message', 'Students Created Successfully.');
65    }
66}
67?>
68