bernandotorrez

KaryawanIndex.php

Oct 21st, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.09 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Livewire\Pages\Karyawan;
  4.  
  5. use Livewire\Component;
  6. use App\Models\Karyawan;
  7. class KaryawanIndex extends Component
  8. {
  9.  
  10.     public $employeeData = [];
  11.  
  12.     /**
  13.      * this is a global variable to work with
  14.      * wire:model="employee_number",
  15.      * wire:model="employee_name"
  16.      * wire:model="gender"
  17.      */
  18.     public $employee_number, $employee_name, $gender;
  19.  
  20.     /**
  21.      * giving validation rule
  22.      * this is a global validation rule
  23.      */
  24.  
  25.     protected $rules = [
  26.         'employee_number'   => 'required|min:10',
  27.         'employee_name'     => 'required|min:5',
  28.         'gender'            => 'required'
  29.     ];
  30.  
  31.     /**
  32.      * will run after render function
  33.      */
  34.     public function mount()
  35.     {
  36.         $this->employeeData = Karyawan::all();
  37.         $this->resetData();
  38.     }
  39.  
  40.     /**
  41.      * This function is to make real-time validation to form, when the user do a change
  42.      */
  43.  
  44.     public function updated($propertyName)
  45.     {
  46.         $this->validateOnly($propertyName);
  47.     }
  48.  
  49.     /**
  50.      * this function will used to reset input field in a form with the wire:model
  51.      */
  52.     public function resetData()
  53.     {
  54.         $this->employee_number = '';
  55.         $this->employee_name = '';
  56.         $this->gender = '';
  57.     }
  58.  
  59.     public function render()
  60.     {
  61.         return view('livewire.pages.karyawan.karyawan-index', [
  62.             'employees' => $this->employeeData
  63.         ]);
  64.     }
  65.  
  66.     /**
  67.      * this function will used to add data to the karyawan table
  68.      */
  69.  
  70.     public function addData() {
  71.  
  72.         // Run the validate function on addData
  73.         $this->validate();
  74.  
  75.         $data = array(
  76.             'employee_number' => $this->employee_number,
  77.             'employee_name' => $this->employee_name,
  78.             'gender' => $this->gender
  79.         );
  80.  
  81.         // Insert data using Eloquent ORM
  82.         Karyawan::create($data);
  83.  
  84.         // Update employeeData with a new Karyawan Data
  85.         $this->employeeData = Karyawan::all();
  86.  
  87.         // Refresh input form
  88.         $this->resetData();
  89.     }
  90. }
  91.  
Add Comment
Please, Sign In to add comment