skip to Main Content

I have two table with relation, defined in model and migration but when I delete the parent data the child do not delete automatically

User model

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;
    use Friendable;
    use HasApiTokens;

    public function userstatus()
    {
        return $this->hasOne(UserStatus::class);
    }
}

UserStatus model

class UserStatus extends Model
{
    public function user()
    {
        return $this->belongsToOne(User::class);
    }

}

User migration:

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('first');
            $table->string('last');
            $table->string('description')->nullable();
            $table->string('phone');
            $table->string('question');
            $table->string('answer');
            $table->string('email')->unique();
            $table->string('country');
            $table->string('city');
            $table->timestamp('email_verified_at')->nullable();
            $table->timestamp('verified')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

UserStatus migration:

class CreateUserStatusesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_statuses', function (Blueprint $table) {
            $table->increments('id');
            $table->foreignId('user_id')->constrained('users')->onDelete('cascade')->onUpdate('cascade');
            $table->string('status')->default('InActive');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('userstatuses');
    }
}

and I also tried using old ways on migration not working. There are a lot of relations in user and I can’t delete them one by one each time so it needs to delete automatically.

3

Answers


  1. Add to your migration:

    $table->engine = 'InnoDB';
    

    See also: https://github.com/laravel/framework/issues/8730

    Login or Signup to reply.
  2. Change the foreign key reference.

    class CreateUserStatusesTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('user_statuses', function (Blueprint $table) {
                $table->increments('id');
                $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
                $table->string('status')->default('InActive');
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('userstatuses');
        }
    }
    
    Login or Signup to reply.
  3. Try to use this one

    public function up()
        {
            Schema::create('user_statuses', function (Blueprint $table) {
                $table->increments('id');
                 $table->engine = "InnoDB";
                 $table->interger('user_id')->unsigned();  
                 $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    
                $table->string('status')->default('InActive');
    
                $table->timestamps();
            });
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search