Home > Software engineering >  How to delete records in 2 tables with just 1 DELETE command in LARAVEL
How to delete records in 2 tables with just 1 DELETE command in LARAVEL

Time:01-18

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"   >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');
}

CodePudding user response:

`

$user ->delete()
employee::where("id", $user->id )->delete();
return redirect()->route('users.index')`

`

CodePudding user response:

 Here is The Blade File


<tbody>
                    <tr>
                    @foreach ($Employes as $employe)
                      <td>{{$employe->id}}</td>
                      <td>{{$employe->name}} </td>

                      <td>{{$employe->slug}}</td>
                      

                      <td>
                        <div >
                          
                          <a href="{{'EmployeDelete/'.$employe->id}}" `enter code here`>
                            Delete
                            <i ></i>                          
                          </a>
                        </div>
                      </td>
                      
                    </tr>
                    @endforeach
                   

WEB.PHP FILE/Route

Route::get('/EmployeDelete/{id}', [App\Http\Controllers\UserController::class, 'EmployeDeleteFun'])->name('EmployeDelete');

HERE IS THE CONTROLLER UserController

public function EmployeDeleteFun($id)
{
    $emp= Employe::findOrFail($id);
    if ($emp) {
        $destination = 'admin/images/post/' . $emp->image;
        if (File::exists($destination)) {
            File::delete($destination);
        }
        $emp->user()->delete();
        $emp->delete();
        return redirect()->back()->with('message', ' Employe deleted succuess');
    } else {
        return redirect()->back()->with('message', 'not deleted');
    }
}

Here is The Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Employe extends Model
{
    use HasFactory;
    protected $table='employes';
    protected $fillable=[
'name',
'Age',
'description',
'img',




    ];
    
    
    public function user(){
        return $this->belongsTo(User::class,'created_by','id');
    }
   
            
           
}


                   
  • Related