通过关联方式 follow 其他用户,创建时间和更新时间没有填充?

代码

class User  extends Authenticatable
{
    ...

    public function followers()
    {
        return $this->belongsToMany(User::class, 'followers', 'user_id', 'follower_id');
    }

    public function followings()
    {
        return $this->belongsToMany(User::class, 'followers', 'follower_id', 'user_id');
    }

    public function follow($user_ids)
    {
        if (!is_array($user_ids)) {
            $user_ids = compact('user_ids');
        }
        $this->followings()->sync($user_ids, false);
    }

    public function unfollow($user_ids)
    {
        if (!is_array($user_ids)) {
            $user_ids = compact('user_ids');
        }
        $this->followings()->detach($user_ids);
    }

    public function isFollowing($user_id)
    {
        return $this->followings->contains($user_id);
    }
}

file

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

在处理 User 和 followers 或者 followings 关联的时候加上 withTimestamps 方法,比如:

public function followers()
{
    return $this->belongsToMany(User::class, 'followers', 'user_id', 'follower_id')
                ->withTimestamps();
}
5年前 评论
Maxwells (楼主) 4年前
讨论数量: 2

在处理 User 和 followers 或者 followings 关联的时候加上 withTimestamps 方法,比如:

public function followers()
{
    return $this->belongsToMany(User::class, 'followers', 'user_id', 'follower_id')
                ->withTimestamps();
}
5年前 评论
Maxwells (楼主) 4年前

在followings方法中加上withTimestamps方法。

public function followings()
    {
        return $this->belongsToMany(User::Class, 'followers', 'follower_id', 'user_id')->withTimestamps();
    }
5年前 评论
Maxwells (楼主) 4年前

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