同步删除回复报错?

使用$thread->replies()->delete();会报错;25 bind or column index out of range (SQL: delete from "replies" where "replies"."thread_id" = App\Reply and "replies"."thread_id" is not null)
换成下面后错误消失,暂不知具体原因
Reply::query()->where('thread_id',$thread->id)->delete();

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 9
jltxwesley

把你Thread Model里的replies关系发出来看看

5年前 评论

@jltxwesley

public function replies()
    {
        return $this->hasMany(Reply::class)
            ->withCount('favorites')
            ->with('owner');
    }
5年前 评论

@jltxwesley
感觉应该是作者遗漏了一些细节,因为我看到后面的这部分内容再次出现的时候两个with都没有了

5年前 评论
jltxwesley

@zh117

后面没有两个with是因为换成了预加载$with array

/**
 * The relationships to always eager-load.
 *
 * @var array
 */
protected $with = ['owner', 'favorites'];
5年前 评论

@jltxwesley
课程里
$with = ['creator'];是这样的,不能加with关系,加了就会报错;

5年前 评论
jltxwesley

@zh117

你可以去github看源代码:https://github.com/laracasts/Lets-Build-a-...
Thread预加载creatorchannel

Reply预加载ownerfavorites

->withCount('favorites')->with('owner')是属于Reply Model,你看的$with = ['creator']是属于Thread Model.

5年前 评论

@jltxwesley 嗯。按照这边教程顺序反正走到我说的报错这一步时,Reply里的内容如下

class Reply extends Model
{
    use Favoritable,RecordsActivity;

    protected $guarded = [];
    protected $with = ['owner','favorites'];

    public function owner()
    {
        return $this->belongsTo(User::class,'user_id');  // 使用 user_id 字段进行模型关联
    }

    public function thread()
    {
        return $this->belongsTo(Thread::class);
    }
}

我上面提到的错误是写在Thread里面的。后面的小伙伴记得这一步删掉Threadreplies()方法下的两个with以解决test报错 :grinning:

5年前 评论
洛未必达

说的很对,遗漏了部分细节,已在本章开头进行说明。

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!