dthtvk3666 2018-09-13 20:57
浏览 56

Laravel侦察algolia非对象

I'm creating a crud application using laravel and using scout with algolia to perform the search functions. However, when I perform scout:import "App\Majors" it throws a "trying to get property of non-object" error. Below is my model.

class Major extends Model
{
    use Searchable;

    protected $fillable = ['title', 'album', 'lyrics', 'youtube_link'];

    public function toSearchableArray()
    {
        $genres = array_map(function($item) {
            return trim($item);
        }, explode(',', $this->college->genres));

        return array_merge( $this->toArray(), ['college' => $this->college->name, 'photo' => $this->college->photo, 'genres' => $genres]);
    }

    public function college()
    {
        return $this->belongsTo('App\College');
    }
}
  • 写回答

1条回答 默认 最新

  • dongmeiyi2266 2018-09-13 21:06
    关注

    I believe the problem is with $this->college. Probably at least one of models does not have college set, and you run:

    1. $this->college->genres
    2. $this->college->name
    3. $this->college->photo

    They will all fail, so you should probably secure your code for such cases, for example instead of:

    explode(',', $this->college->genres)
    

    you should use:

    explode(',', $this->college ? $this->college->genres : '')
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用