Laravel RelationShips 遇到 must return a relationship instance

Laravel的关联函数,要求必须返回 relationship instance,而不能返回 false[],最终处理为未定义 module_name 时,生成一个随机的字符串作为 module 字段值进行过滤。

public function shareEntries()
{
    if (! isset($this->module_name)) {
        // 如果未定义模型名,随机生成一个字符串。为了满足 must return a relationship instance
        $where['module'] = randomStr(16);
    } else {
        $where['module'] = $this->module_name;
    }

    return $this->hasMany(ShareEntry::class, 'model_id', 'id')->where($where);
}

发表评论

您的电子邮箱地址不会被公开。