3 min read

Laravel polymorphic searching

Filter by parent morph models types:

           
 // Model: Comment
 
 /**
 * Get the parent commentable model (post or video).
 */
 public function subject(): MorphTo
 {
     return $this->morphTo();
 }
 
 public function scopeBlogs()
 {
     return $this->whereHasMorph('subject', [Blog::class]);
 }

        

I will be writing an article on ‘global searching’ and rendering results on the front-end soon but in the meantime checkout the Laravel Cross Eloquent Search by ProtoneMedia or searching across eloquent models.