同步删除回复报错?

使用$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();

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 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年前 评论

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