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

Warning about DB::listen #9091

Closed
wants to merge 3 commits into from
Closed

Warning about DB::listen #9091

wants to merge 3 commits into from

Conversation

decadence
Copy link
Contributor

@decadence decadence commented Oct 22, 2023

I noticed a limitation: inside DB::listen you cannot use data insertion into the database, because in this case the insertion from the Eloquent side breaks due to the fact that LAST_INSERT_ID returns the ID not for the original record, but for the one that is inserted inside DB::listen.

For example if you want to log slow queries:

DB::listen(function (QueryExecuted $query) {
    if($query->time > 500) {
        SlowQuery::create([
            "sql" => $query->sql
        ]);
    }
});

$post = Post::create(...); // if this takes more than 500 ms
$post->getKey(); // ID will be from SlowQuery model, not from Post

Not so easy to detect this immediately.

database.md Outdated Show resolved Hide resolved
Co-authored-by: Artur Gauzer <artur.gauzer@pm.me>
@decadence
Copy link
Contributor Author

Looks like this should be obvious for any Laravel programmer 🤷
So good luck for those who will use insertion in DB::listen 🫡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants