bernandotorrez

KaryawanIndex.php

Oct 22nd, 2020
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 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. public $show_update_form = false;
  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->reset(['employee_number', 'employee_name', 'gender']);
  55. }
  56.  
  57. public function render()
  58. {
  59. return view('livewire.pages.karyawan.karyawan-index', [
  60. 'employees' => $this->employeeData
  61. ]);
  62. }
  63.  
  64. /**
  65. * this function will used to add data to the karyawan table
  66. */
  67.  
  68. public function addData()
  69. {
  70. // Run the validate function on addData
  71. $this->validate();
  72.  
  73. $data = array(
  74. 'employee_number' => $this->employee_number,
  75. 'employee_name' => $this->employee_name,
  76. 'gender' => $this->gender
  77. );
  78.  
  79. // Insert data using Eloquent ORM
  80. Karyawan::create($data);
  81.  
  82. // Update employeeData with a new Karyawan Data
  83. $this->employeeData = Karyawan::all();
  84.  
  85. // Refresh input form
  86. $this->resetData();
  87. }
  88.  
  89. /**
  90. * show Data that want to be update
  91. */
  92.  
  93. public function showUpdateForm($employeeNumber)
  94. {
  95. $this->show_update_form = true;
  96.  
  97. $karyawan = Karyawan::where('employee_number', $employeeNumber)->first();
  98.  
  99. $this->employee_number = $karyawan->employee_number;
  100. $this->employee_name = $karyawan->employee_name;
  101. $this->gender = $karyawan->gender;
  102. }
  103.  
  104. /**
  105. * Update data function
  106. */
  107. public function updateData()
  108. {
  109. $data = array(
  110. 'employee_name' => $this->employee_name,
  111. 'gender' => $this->gender
  112. );
  113.  
  114. Karyawan::where('employee_number', $this->employee_number)->where('status', 1)->update($data);
  115.  
  116. $this->show_update_form = false;
  117. $this->resetData();
  118. $this->employeeData = Karyawan::all();
  119. }
  120. }
  121.  
Add Comment
Please, Sign In to add comment