Askedio / laravel-soft-cascade

Cascade Delete & Restore when using Laravel SoftDeletes
https://medium.com/asked-io/cascading-softdeletes-with-laravel-5-a1a9335a5b4d
MIT License
705 stars 63 forks source link

Deleting wrong relationship record #91

Closed cacpmw closed 4 years ago

cacpmw commented 5 years ago

In my system a Student is a User. After the following code a user was actually deleted. However, it took the students id and used it on the user table, thus, deleting the wrong user.

This is my controller's method

try {
            DB::transaction(function () use ($id) {
                Student::findOrFail($id)->delete();
            }, 5);
            return redirect()->action('StudentController@index');
        } catch (ModelNotFoundException $exception) {
            return redirect()->action('StudentController@index');
        }

This is my Student class

<?php

namespace App;

use Askedio\SoftCascade\Traits\SoftCascadeTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Student extends Model
{
    use SoftCascadeTrait;
    use SoftDeletes;
    protected $softCascade = ['user'];
    protected $dates = ['deleted_at'];
    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

Am I missing anything?

maguilar92 commented 5 years ago

@cacpmw Could you pass schema of your users and students tables? Only need primary key and relationship columns. Also I need your User class.

cacpmw commented 5 years ago

@maguilar92 here's what you asked. Thank you in advance.

USER SCHEMA

Schema::create('users', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('role_id')->unsigned()->index();
            $table->string('name');
            $table->string('email')->unique();
            $table->dateTime('email_verified_at')->nullable();
            $table->string('password');
            $table->string('remember_token')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

STUDENTS SCHEMA

    Schema::create('students', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('user_id')->unsigned()->index();
            $table->integer('registration_id')->unsigned()->index();
            $table->string('private_number')->unique();
            $table->string('emergency_number')->index();
            $table->date('birthday');
            $table->string('address');
            $table->string('occupation')->index()->comment('job title');
            $table->string('ssn')->unique();
            $table->string('personal_id')->unique();
            $table->string('marital_status')->index();
            $table->integer('timetable_ticket');
            $table->integer('reschedule_ticket');
            $table->timestamps();
            $table->softDeletes();
        });

USER CLASS:

<?php

namespace App;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use SoftDeletes;
    use Notifiable;
    protected $dates = ['deleted_at'];

    protected $fillable = [
        'name', 'email', 'password', 'role_id'
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    public function role()
    {
        return $this->belongsTo('App\Role');
    }

    public function student()
    {
        return $this->hasOne('App\Student');
    }

    public function isAdmin()
    {
        if ($this->role->id === 1) {
            return true;
        }
        return false;
    }
    public function isStudent()
    {
        if ($this->role->id === 4) {
            return true;
        }
        return false;
    }
    public function isIntern()
    {
        if ($this->role->id === 3) {
            return true;
        }
        return false;
    }
}
maguilar92 commented 5 years ago

@cacpmw Thank you for the information. I forgot to ask you what laravel version and laravel soft cascade version are you using.

cacpmw commented 5 years ago

@cacpmw Thank you for the information. I forgot to ask you what laravel version and laravel soft cascade version are you using.

Both the latest versions

cacpmw commented 5 years ago

@maguilar92 any progress on this issue?

maguilar92 commented 5 years ago

@cacpmw Sorry but I've been tied by work. I will review it as soon as possible.

maguilar92 commented 5 years ago

@cacpmw Also you can search for the error that you comment and open a pull request with the fix.