Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ErikRobles's avatar

Delete Record and corresponding records Cascade Confusion Laravel

Hello. I am having a hard time wrapping my mind on how to go about doing this. I am deleting a student from our database. BUT... our student has a progress record elsewhere in the db that also needs to dissapear. I tried modifying the migration table to handle this but I am obviously getting it wrong. I get the null error when trying to visit the progress view. So, if anyone can help me get a better handle on how this is done, I would sure appreciate it. Here are my two migration tables: PerformanceTable:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePerformancesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('performances', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('student_id')->nullable();
            $table->date('date')->nullable();
            $table->integer('teacher_id')->nullable();
            $table->string('last_unit_covered')->nullable();
            $table->integer('last_page_viewed')->nullable();
            $table->string('exam_type')->nullable();
            $table->integer('exam_score')->nullable();
            $table->text('comments')->nullable();
            $table->timestamps();
        });
    }

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

Students Migration:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateStudentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('company_id')->nullable();
            $table->integer('level_id')->nullable();
            $table->integer('teacher_id')->comment('Teacher_id=User_id')->nullable();
            $table->foreign('student_id')->references('id')->on('performances')->onDelete('cascade');
            $table->timestamps();
        });
    }

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

Thank you in advance for your help on this as, to me, it's a real mind bender for me (not as bad as relationships).

0 likes
3 replies
Nakov's avatar
Nakov
Best Answer
Level 73

You need this:

$table->foreign('student_id')->references('id')->on('performances')->onDelete('cascade');

in your performances table migration, not in the students.

And it can be simplified:

$table->foreignId('student_id')->nullable()->constrained()->cascadeOnDelete();

and remove this

$table->unsignedBigInteger('student_id')->nullable();

or if you use the first approach you have an error there too.. it should be this:

$table->foreign('student_id')->references('id')->on('students')->onDelete('cascade');

as the table is students to which the id refers to.

1 like
ErikRobles's avatar

@Nakov Thank you for your reply Nakov. I am testing it now. I am sure it will work. Will let you know soon.

ErikRobles's avatar

@Nakov Thank you. That is exactly what I was trying to do. Thanks a million.

Please or to participate in this conversation.