Call to a member function tags() on null 多对多关联无关联数据时报错

class Locksmith extends ApiModel
{
    /**
     * @var string
     */
    protected $table = 'locksmith';

    /**
     * @var string
     */
    protected $primaryKey = 'locksmith_id';

    /**
     * 表明模型是否应该被打上时间戳
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * 关联查找用户档案信息
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function archive()
    {
        return $this->belongsTo('App\Models\Archive', 'user_id', 'archive_id');
    }

    /**
     * 多对多查找评价标签.
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function tags()
    {
        return $this->belongsToMany('App\Models\EvaluateTag', 'evaluate_tag_master', 'master_id', 'tag_id')->withPivot('count');
    }

}

在调用时$tag = Locksmith::find($locksmithId)->tags()->get()->toArray();会报:Call to a member function tags() on null

public function tags()是一个多对多的关联 当关联表evaluate_tag_master有数据时不会报错
这个怎么办啊?

本地服务器上不报错 服务器上要报错 框架都是更新到最新的了环境一样。

阅读 4.1k
2 个回答

这个错误的原因是Locksmith::find($locksmithId)没有查询到结果,返回了null造成的。

原因是 关联多对多的主键问题错误产生的。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题