Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Allow running observer callbacks after database transactions have committed #440

Merged
merged 1 commit into from
Jan 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions config/scout.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@

'queue' => env('SCOUT_QUEUE', false),

/*
|--------------------------------------------------------------------------
| Database Transactions
|--------------------------------------------------------------------------
|
| This option allows you to control if the operations that sync your
| data with your search engines are only executed afterall open
| database transactions have committed.
|
*/

'after_commit' => false,

/*
|--------------------------------------------------------------------------
| Chunk Sizes
Expand Down
17 changes: 17 additions & 0 deletions src/ModelObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,30 @@

class ModelObserver
{
/**
* Only dispatch the observer's events after all database transactions have committed.
*
* @var bool
*/
public $afterCommit;

/**
* The class names that syncing is disabled for.
*
* @var array
*/
protected static $syncingDisabledFor = [];

/**
* Create a new observer instance.
*
* @return void
*/
public function __construct()
{
$this->afterCommit = config('scout.after_commit', false);
}

/**
* Enable syncing for the given class.
*
Expand Down