I'm working in a Laravel 8 project where I've got a model called PingTest
and a linked model called PingTestEntry
.
When a PingTest
is deleted, either through soft deletes (which I've got set up) or through a forced delete, I expect all of my PingTestEntry
records to be deleted too, but this isn't happening and I'm struggling to realise why.
I shall attach my models
PingTest
<?php
namespace App\Models\Tools;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
class PingTest extends Model
{
use HasFactory, SoftDeletes;
/**
* Indicates if the model's ID is auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'ping_tests';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'id',
'url',
'shareable_id'
];
/**
* The relationships that should always be loaded.
*
* @var array
*/
protected $with = [
'entries'
];
/**
* Get the comments for the blog post.
*/
public function entries()
{
return $this->hasMany(PingTestEntry::class, 'test_id')->orderBy('created_at', 'asc');
}
/**
* Model to get
*/
public function pingTestEntries() {
return $this->hasMany(PingTestEntry::class);
}
/**
* The "booted" method of the model.
*
* @return void
*/
protected static function booted()
{
static::deleted(function ($model) {
$model->pingTestEntries()->delete();
});
}
}
PingTestEntry
<?php
namespace App\Models\Tools;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class PingTestEntry extends Model
{
use HasFactory;
/**
* Indicates if the model's ID is auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'ping_test_entries';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'id',
'test_id',
'reply_from',
'bytes',
'time',
'ttl'
];
/**
* Get the PingTest that owns the comment.
*/
public function pingTest()
{
return $this->belongsTo(PingTest::class);
}
}
Then, in Tinker, but could easily be through a task/cron, I'm deleting a PingTest
which auto-joins with the with
relation the entries:
App\Models\Tools\PingTest::where('id', '79b2aa35-89ce-46d7-93c9-3fda0e0a1417')->delete();
What am I missing/need to change here?