PHPackages                             foxws/laravel-scout-relations - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. foxws/laravel-scout-relations

ActiveLibrary[Database &amp; ORM](/categories/database)

foxws/laravel-scout-relations
=============================

Automatically re-index Scout-searchable related models.

0.0.2(1mo ago)11.0k↓44.7%MITPHPPHP ^8.4CI passing

Since Apr 12Pushed 1mo agoCompare

[ Source](https://github.com/foxws/laravel-scout-relations)[ Packagist](https://packagist.org/packages/foxws/laravel-scout-relations)[ Docs](https://github.com/foxws/laravel-scout-relations)[ GitHub Sponsors](https://github.com/Foxws)[ RSS](/packages/foxws-laravel-scout-relations/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (2)Dependencies (13)Versions (3)Used By (0)

Laravel Scout Relations
=======================

[](#laravel-scout-relations)

[![Latest Version on Packagist](https://camo.githubusercontent.com/bd326bf2be8211b02534142126a31cf70713fcfe0604c5799b77daeae93f200d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f666f7877732f6c61726176656c2d73636f75742d72656c6174696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/foxws/laravel-scout-relations)[![GitHub Tests Action Status](https://camo.githubusercontent.com/86b3a9407c13c5d0062bd28452d7933c06775ffd2f16925a37e800df91f58ad2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f666f7877732f6c61726176656c2d73636f75742d72656c6174696f6e732f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/foxws/laravel-scout-relations/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/c7d04be99eeca31200c9da8cc0594f429c31e8b51601cecd4633dab478a49e42/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f666f7877732f6c61726176656c2d73636f75742d72656c6174696f6e732f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/foxws/laravel-scout-relations/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/0c6756bdc2d9d33b7ef11def632c759eaf4b1de122542b696140b870891c0be2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f666f7877732f6c61726176656c2d73636f75742d72656c6174696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/foxws/laravel-scout-relations)

Automatically re-index Scout-searchable related models when an Eloquent model is saved or deleted.

When a parent model changes (e.g. an `Author`), its related Searchable models (e.g. `Post`) are automatically queued for re-indexing, keeping your search index consistent without any manual intervention.

Requirements
------------

[](#requirements)

- PHP 8.4+
- Laravel 12+
- [Laravel Scout](https://laravel.com/docs/scout)

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require foxws/laravel-scout-relations
```

Usage
-----

[](#usage)

Add the `HasSearchableRelations` trait to any Eloquent model whose changes should trigger re-indexing of related models. Then override `searchableRelations()` to return the relationship names to watch.

```
use Foxws\ScoutRelations\Concerns\HasSearchableRelations;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Author extends Model
{
    use HasSearchableRelations;

    /**
     * Relationships whose models should be re-indexed when this model changes.
     *
     * @return array
     */
    public function searchableRelations(): array
    {
        return ['posts'];
    }

    public function posts(): HasMany
    {
        return $this->hasMany(Post::class);
    }
}
```

The related `Post` model must use Laravel Scout's `Searchable` trait:

```
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class Post extends Model
{
    use Searchable;

    public function toSearchableArray(): array
    {
        return [
            'id'          => $this->id,
            'title'       => $this->title,
            'author_name' => $this->author->name, // kept fresh on every re-index
        ];
    }
}
```

Now whenever an `Author` is saved with changes or deleted, all of its `Post` records are automatically re-indexed.

How it works
------------

[](#how-it-works)

The trait hooks into Eloquent's `saved` and `deleted` model events:

- **`saved`** — re-indexes relations only when `wasChanged()` is `true`, avoiding unnecessary indexing on no-op saves.
- **`deleted`** — re-indexes relations unconditionally so the search index reflects the parent's removal.

Re-indexing is performed in chunks via `chunkById`. If the related model defines `makeAllSearchableUsing()`, it is applied to the chunk query, preventing N+1 queries.

A per-class re-entry guard prevents infinite cascades when mutual relationships exist.

Configuration
-------------

[](#configuration)

Publish the config file with:

```
php artisan vendor:publish --tag="scout-relations-config"
```

Available options in `config/scout-relations.php`:

KeyEnv variableDefaultDescription`enabled``SCOUT_RELATIONS_ENABLED``true`Disable all automatic relation syncing`chunk.searchable``SCOUT_RELATIONS_CHUNK_SEARCHABLE``500`Chunk size for `searchable()` calls`chunk.unsearchable``SCOUT_RELATIONS_CHUNK_UNSEARCHABLE``500`Chunk size reserved for future useArtisan command
---------------

[](#artisan-command)

To manually force re-indexing of all relations for a model (ignoring the `enabled` flag), use:

```
php artisan scout:index-relations "App\Models\Author"
```

This chunks through every record of the given model and calls `reindexSearchableRelations()` on each, with a progress bar.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [francoism90](https://github.com/foxws)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

43

—

FairBetter than 89% of packages

Maintenance91

Actively maintained with recent releases

Popularity22

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 75% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~9 days

Total

2

Last Release

49d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5028905?v=4)[François M.](/maintainers/francoism90)[@francoism90](https://github.com/francoism90)

---

Top Contributors

[![francoism90](https://avatars.githubusercontent.com/u/5028905?v=4)](https://github.com/francoism90 "francoism90 (6 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (1 commits)")

---

Tags

algoliaeloquentindexinglaravellaravel-packagelaravel-scoutmeilisearchscoutsearchtypesenselaravelfoxwslaravel-scout-relations

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/foxws-laravel-scout-relations/health.svg)

```
[![Health](https://phpackages.com/badges/foxws-laravel-scout-relations/health.svg)](https://phpackages.com/packages/foxws-laravel-scout-relations)
```

###  Alternatives

[spatie/laravel-pdf

Create PDFs in Laravel apps

1.0k4.3M41](/packages/spatie-laravel-pdf)[wnx/laravel-backup-restore

A package to restore database backups made with spatie/laravel-backup.

210389.8k2](/packages/wnx-laravel-backup-restore)[spatie/laravel-model-flags

Add flags to Eloquent models

4341.2M4](/packages/spatie-laravel-model-flags)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

436834.4k1](/packages/clickbar-laravel-magellan)[rawilk/profile-filament-plugin

Profile &amp; MFA starter kit for filament.

3913.7k](/packages/rawilk-profile-filament-plugin)[lacodix/laravel-model-filter

A Laravel package to filter, search and sort models with ease while fetching from database.

17555.1k](/packages/lacodix-laravel-model-filter)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
