【问题标题】:Laravel related model not deleting when running delete method运行删除方法时未删除 Laravel 相关模型
【发布时间】:2021-10-31 21:42:24
【问题描述】:

我在一个 Laravel 8 项目中工作,我有一个名为 PingTest 的模型和一个名为 PingTestEntry 的链接模型。

PingTest 被删除时,无论是通过软删除(我已经设置好了)还是通过强制删除,我希望我的所有 PingTestEntry 记录也会被删除,但这不会发生我很难理解为什么。

我会附上我的模型

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);
    }
}

然后,在 Tinker 中,但可以很容易地通过任务/cron,我删除了一个 PingTest,它与条目的 with 关系自动加入:

App\Models\Tools\PingTest::where('id', '79b2aa35-89ce-46d7-93c9-3fda0e0a1417')->delete();

我在这里缺少/需要更改什么?

【问题讨论】:

  • 检查this
  • 如果您向下滚动到我的PingTest 模型的末尾,您会看到我已经在执行您在booted 函数中附加的内容。请注意,您链接的文章是几年前的文章,如上所述,我使用的是最新版本的 Laravel,版本 8。

标签: php laravel


【解决方案1】:

在设计数据库表时使用外键。 将test_id 设为外键

【讨论】:

  • 大声笑,它是:$table-&gt;foreignUuid('test_id');
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-01
  • 2014-06-09
  • 1970-01-01
  • 1970-01-01
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多