1

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?

Ryan H
  • 2,620
  • 4
  • 37
  • 109
  • check [this](https://stackoverflow.com/a/32163898/7574023) – boolfalse Oct 31 '21 at 13:24
  • If you scroll down to the end of my `PingTest` model, you'll see that I already am doing what you've attached in the `booted` function. Note that the article you've linked was from several years ago, and as noted, I'm using the latest version of Laravel, version 8. – Ryan H Oct 31 '21 at 13:29

1 Answers1

-1

Use foreign key when you design your database table. Make test_id to foreign key

Ronny Dsouza
  • 358
  • 2
  • 12