0

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');
    }
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Damian Buttle
  • 75
  • 3
  • 11

1 Answers1

0

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 : '')
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291