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).