php - Laravel Scout algolia 非对象
问题描述
我正在使用 laravel 创建一个 crud 应用程序,并使用 scout 和 algolia 来执行搜索功能。但是,当我执行 scout:import "App\Majors" 时,它会引发“尝试获取非对象的属性”错误。下面是我的模型。
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');
}
}
解决方案
我相信问题出在$this->college
. 可能至少有一个模型没有大学设置,你运行:
- $this->college->genres
- $this->college->name
- $这个->大学->照片
它们都会失败,因此您可能应该在这种情况下保护您的代码,例如,而不是:
explode(',', $this->college->genres)
你应该使用:
explode(',', $this->college ? $this->college->genres : '')
推荐阅读
- javascript - 常规函数与箭头函数中的“this”范围
- artificial-intelligence - 有没有一种方法可以从运行在虚幻引擎上的游戏中访问游戏时间变量,例如健康条和其他分数
- ios - 由于 pod unity-iphone-framework.sh 第 42 行,xcode 构建未编译来源:未绑定变量
- javascript - Spark AR - 无法使用 Scene.create() 方法创建场景对象
- c# - 您在哪里使用 .NET Core 将 ViewModel 存储在 MVC 中?
- java - 当我尝试返回具体对象时,泛型代码不起作用
- javascript - 有没有办法在 eventdrop 上更改 fullcalender 版本 5.9.0 中的日期时间格式
- swift - 尝试将项目提交到 App Store 后出现 MDFInternationalization 错误
- python - Python 正则表达式提取电子邮件信息
- javascript - 根据选择更新字段