Search code examples
laravelsoft-delete

How can I use Softdeletes in the models of Employee and User?


Employee Migration

public function up()
{
    Schema::create('employees', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 60);
        $table->string('phone_whats', 30)->nullable();
        $table->string('phone_home', 30)->nullable();
        $table->string('email', 255)->nullable();
        $table->string('dt_birthday', 20)->nullable();
        $table->string('zipcode', 20)->nullable();
        $table->integer('id_city')->unsigned();
        $table->integer('id_state')->unsigned();
        $table->string('address', 255)->comment('Endereço')->nullable();
        $table->string('number', 10)->nullable();
        $table->string('rg', 25)->nullable();
        $table->string('cpf', 20)->nullable();
        $table->string('password', 255)->nullable();
        $table->foreign('id_city')->references('id')->on('cities');
        $table->foreign('id_state')->references('id')->on('states');
        $table->timestamps();
        $table->softDeletes();
    });
}    
public function down()
{
    Schema::dropIfExists('employees');
}

User Migration

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->Increments('id');
        $table->string('name',255);
        $table->integer('id_employee')->unsigned();
        $table->string('email',255)->unique();
        $table->string('password',255);
        $table->foreign('id_employee')->references('id')->on('employees');
        $table->timestamps();
        $table->softDeletes();
    });
}
public function down()
{
    Schema::dropIfExists('users');
}

My Model Employee

<?php

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;

class Employee extends Model
{
    use SoftDeletes;
    protected $hidden = ['created_at', 'deleted_at', 'updated_at'];
    protected $dates = ['deleted_at'];
    protected $fillable = [
        'name', 'phone_home', 'phone_whats','email', 'dt_birthday', 'number', 'rg', 'cpf', 'address',
        'id_state', 'id_city', 'password'
    ];
}

My Model User

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends Model
{
    use SoftDeletes;
}

Employee Controller

public function destroy(Employee $employee)
{
        $employee->delete();
        return redirect()->back();
}

I would like to use softdeletes in the registration of employees and users, when I tested only in the Employee Controller register, my database registration was deleted, I just wanted to apply softdeletes in the employee and the user in the same function, keeping the registration in the database and just fill in the delete_at columns of the two databases leaving them inactive.


Solution

  • Looking at the code, it should work - have you runned migrations (if you have added softDeletes later)? How do you know your record was deleted? Have you checked it directly in database, or via Eloquent? As SoftDeletes trait implictly adds to every query clause, returning only models which are not deleted softly, so via User::find() you wouldn't get model that was deleted softly.