| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- class Member extends Component
- {
- public $members, $first_name, $last_name, $status, $birth_city_id, $birth_province_id, $birth_nation_id, $birth_date, $gender, $fiscal_code, $address, $zip_code, $nation_id, $province_id, $city_id, $phone, $email, $enabled, $updateMember = false, $addMember = false;
- protected $listeners = [
- 'deleteMemberListner'=>'deleteMember'
- ];
- protected $rules = [
- 'first_name' => 'required',
- 'last_name' => 'required',
- ];
- public function resetFields(){
- $this->first_name = '';
- $this->last_name = '';
- $this->status = '';
- $this->birth_city_id = null;
- $this->birth_province_id = null;
- $this->birth_nation_id = null;
- $this->birth_date = null;
- $this->gender = '';
- $this->fiscal_code = '';
- $this->address = '';
- $this->zip_code = '';
- $this->nation_id = null;
- $this->province_id = null;
- $this->city_id = null;
- $this->phone = '';
- $this->email = '';
- $this->enabled = true;
- }
- public function render()
- {
- $this->members = Member::select('id', 'first_name', 'last_name', 'status', 'enabled')->get();
- return view('livewire.member');
- }
- public function addMember()
- {
- $this->resetFields();
- $this->addMember = true;
- $this->updateMember = false;
- }
- public function storeMember()
- {
- $this->validate();
- try {
- Member::create([
- 'first_name' => $this->first_name,
- 'last_name' => $this->last_name,
- 'status' => $this->status,
- 'birth_city_id' => $this->birth_city_id,
- 'birth_province_id' => $this->birth_province_id,
- 'birth_nation_id' => $this->birth_nation_id,
- 'birth_date' => $this->birth_date,
- 'gender' => $this->gender,
- 'fiscal_code' => $this->fiscal_code,
- 'address' => $this->address,
- 'zip_code' => $this->zip_code,
- 'nation_id' => $this->nation_id,
- 'province_id' => $this->province_id,
- 'city_id' => $this->city_id,
- 'phone' => $this->phone,
- 'email' => $this->email,
- 'enabled' => $this->enabled
- ]);
- session()->flash('success, $Tesserato creato');
- $this->resetFields();
- $this->addMember = false;
- } catch (\Exception $ex) {
- session()->flash('error','Errore in fase di salvataggio');
- }
- }
- public function editMember($id){
- try {
- $member = Member::findOrFail($id);
- if( !$member) {
- session()->flash('error','Tesserato non trovato');
- } else {
- $this->first_name = $member->first_name;
- $this->last_name = $member->last_name;
- $this->status = $member->status;
- $this->birth_city_id = $member->birth_city_id;
- $this->birth_province_id = $member->birth_province_id;
- $this->birth_nation_id = $member->birth_nation_id;
- $this->birth_date = $member->birth_date;
- $this->gender = $member->gender;
- $this->fiscal_code = $member->fiscal_code;
- $this->address = $member->address;
- $this->zip_code = $member->zip_code;
- $this->nation_id = $member->nation_id;
- $this->province_id = $member->province_id;
- $this->city_id = $member->city_id;
- $this->phone = $member->phone;
- $this->email = $member->email;
- $this->enabled = $member->enabled;
- $this->memberId = $member->id;
- $this->updateMember = true;
- $this->addMember = false;
- }
- } catch (\Exception $ex) {
- session()->flash('error','Errore');
- }
- }
- public function updateMember()
- {
- $this->validate();
- try {
- Member::whereId($this->memberId)->update([
- 'first_name' => $this->first_name,
- 'last_name' => $this->last_name,
- 'status' => $this->status,
- 'birth_city_id' => $this->birth_city_id,
- 'birth_province_id' => $this->birth_province_id,
- 'birth_nation_id' => $this->birth_nation_id,
- 'birth_date' => $this->birth_date,
- 'gender' => $this->gender,
- 'fiscal_code' => $this->fiscal_code,
- 'address' => $this->address,
- 'zip_code' => $this->zip_code,
- 'nation_id' => $this->nation_id,
- 'province_id' => $this->province_id,
- 'city_id' => $this->city_id,
- 'phone' => $this->phone,
- 'email' => $this->email,
- 'enabled' => $this->enabled
- ]);
- session()->flash('success','Tesserato aggiornato');
- $this->resetFields();
- $this->updateMember = false;
- } catch (\Exception $ex) {
- session()->flash('success','Errore');
- }
- }
- public function cancelMember()
- {
- $this->addMember = false;
- $this->updateMember = false;
- $this->resetFields();
- }
- public function deleteMember($id)
- {
- try{
- Member::find($id)->delete();
- session()->flash('success',"Tesserato eliminato");
- }catch(\Exception $e){
- session()->flash('error',"Errore");
- }
- }
- }
|