skip to Main Content

Table structure model:

Table: user

id
name
email

Table: employee

id
user_id
cpf

We have these two tables in the database, how to make a script, which takes the id of the user table and checks in the employee table, if the user_id are the same, if it is the same it deletes the record from the two tables, if it is not the same it deletes the record only from the user table

Rota

Route::delete('/plano/{id}',[ChangePlanController::class, 'destroy'])->name('plan.destroy');

views

<form action="{{ route('plan.destroy', $profissional->user_id)}}" method="POST">
    @method('DELETE')
    @csrf
    <button type="submit"  class="text-indigo-600 hover:text-indigo-900" >Excluir</button>
</form>

Controller

public function destroy($id)
{
    if(!$user = UserEmployee::find($id))
     
    return redirect()->route('plan.searchUsers');
    
    $user->delete();
    
    return redirect()->route('plan.searchUsers');
}

2

Answers


  1. $user ->delete()
    employee::where("id", $user->id )->delete();
    return redirect()->route('users.index')`
    
    Login or Signup to reply.
  2. Use this method on your controller

        if($user= UserEmployee::findOrFail($id)){ 
               $user->user()->delete();
                $user->delete();
                return redirect()->back("message","Deleted");
        }else{
               return redirect()->back("message","Not Deleted");
    }
    

    Please Make Sure yor model have Relationship if not please try to make Relationship b/n Tables

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search