2021年4月6日星期二

Insert, update, and delete 2 table with 1 form

I have users table with name, username, email, password, level and profiles table with name, profile_code, id_class

Logic

  1. profiles.name = users.name, profiles.profile_code = users.username = users.password
  2. So I can insert edit and delete on both table for that field

Code

Form controller
protected $listeners=[ 'getModalId' ];    public function getModalId($idUpdate)  {      $this->idAction=$idUpdate;      $idprofile=profile::find($this->idAction);      $idUser=User::find($this->idAction);      $this->profile_code=$idprofile->profile_code;      $this->name=$idprofile->name;      $this->class=$idprofile->id_class;       $this->name=$this->name;      $this->password=Hash::make($this->profile_code);      $this->username=$this->profile_code;      $this->email=$idUser->email;  }    public function save()  {      $data=[          'profile_code'=>$this->profile_code,          'name'=>$this->name,          'id_class'=>$this->class,          'username'=>$this->nisn,          'password'=>Hash::make($this->profile_code),          'name'=>$this->name,          'email'=>$this->email,          'level'=>'profile'      ];            $idData=['id'=>$this->idAction];      $idDataLogin=['id'=>$this->idAction];            $dataUpdateProfile=profile::where('id',$idData)->first();      $dataUpdateProfileLogin=User::where('id',$idDataLogin)->first();        if( $dataUpdateProfile == null && $dataUpdateProfileLogin == null){          profile::create($data);          $this->resetVar($data);          User::create($data);          $this->resetVar($data);      }else{          profile::find($idData)->first()->update($data);          User::find($idDataLogin)->first()->update($data);      }  }  

Component Controller

public function selectedItem($idAction, $action)  {      $this->idItem=$idAction;      if ($action == 'delete'){          $this->emit('getModalId', $this->idItem);          $this->dispatchBrowserEvent('openDeleteProfile');      }else{          $this->emit('getModalId', $this->idItem);          $this->dispatchBrowserEvent('openUpdateProfile');      }  }    public function delete()  {      profile::destroy($this->idItem);      User::destroy($this->idItem);      $this->dispatchBrowserEvent('closeDeleteModal');      return redirect()->route('profile');  }  

Profile Model

protected $table="profiles";  protected $fillable=[      'profile_code',      'name',      'id_class'  ];  

Problem

With my code, I can add data to the database successfully on both tables but when I edit and remove the data, it only does it on profiles table. When I change the users id to the same as profiles id manually I can edit and remove on users table, it seems like it finds the id on users table same value as id on profiles table. How do I fix this or is there any cleaner way to do this? https://stackoverflow.com/questions/66972190/insert-update-and-delete-2-table-with-1-form April 06, 2021 at 11:59PM

没有评论:

发表评论