Skip to content

Conversation

stevebauman
Copy link
Contributor

@stevebauman stevebauman commented Jun 27, 2025

This is a small change to prevent an unnecessary query from being executed when calling syncWithoutDetaching with an empty value or collection (collect(), [], or null).

Without this, we have to check every time to ensure the values are present instead of the framework automatically handling this. Over time, this can reduce a lot of conditions from being created unnecessarily, as well as SQL queries.

Before:

if ($tags->isNotEmpty()) {
    $post->tags()->syncWithoutDetaching($tags);
}

After:

$post->tags()->syncWithoutDetaching($tags);

@taylorotwell taylorotwell merged commit 4065ab7 into laravel:12.x Jun 28, 2025
60 checks passed
mohammad-fouladgar pushed a commit to mohammad-fouladgar/framework that referenced this pull request Jul 22, 2025
…hing` method when empty values are given (laravel#56157)

* Prevent unnecessary sync when IDs are empty and detaching is false

* Add test for sync with empty array and detaching disabled

* Test with all empty values

* Rearrange precedence of methods

* Tweak test

* Disable query log after sync test
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.

2 participants