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

查看:254
本文介绍了php - 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有数据时不会报错
这个怎么办啊?

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

解决方案

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

这篇关于php - Call to a member function tags() on null 多对多关联无关联数据时报错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆