-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Using firstWhere() inside when() does not yield the expected result #44712
Comments
Heya, thanks for reporting. We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up. laravel new bug-report --github="--public" Please do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue. Thanks! |
I was able to reproduce the test. This is intended behavior in my opinion, a callback is any reference to executable code that is passed as an argument to another piece of code; that code is expected to call back the callback function as part of it jobs. When an expression is true, it should execute a piece of code, but callbacks aren't made for returning values. This is also notable in the documentation when looking at how the when methods are used. When an expression is true, we push something into a collection or we execute another piece of code. I do have to say that I was confused by this too, but after looking way too much at the Laravel codebase (especially Taylors code), you get really comfortable with callbacks. It would be a dream to get the inner value back, but that would demolish codebases. |
I don't agree with you, but even if that was the intended behaviour, then it's not BC, as the test above passes in Laravel 8.83.25. It should have at least being mentioned in the upgrade notes. You also said that "callbacks are not meant to return values", but if For example, the following code does return the proper record. $collection->when(
true,
fn ($collection) => $collection->firstWhere('name', '=', 'John')
); Also, I don't think it's much about the callback returning a value but rather the callback actually changing the original collection. But as I said I don't think it's intended behaviour, for a couple of reasons.
<?php
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
class FirstTest extends TestCase
{
public function test_first(): void
{
$collection = collect([
['name' => 'John'],
['name' => 'Paul'],
]);
$john = $collection->first();
$this->assertNotNull($john);
$stillJohn = $collection->when(
true,
fn ($collection) => $collection->first()
);
$this->assertNotNull($stillJohn);
}
}
User::factory()->create(['name' => 'John']);
User::factory()->create(['name' => 'Paul']);
$peterQuery = User::query()->when(
true,
fn (Builder $query) => $query->firstWhere('name', '=', 'Peter')
); The SQL for @driesvints |
@troccoli @driesvints Thanks for the clear explainer. After diving into the code, this is caused due to the null coalescing operator returning $this, where $this refers to the collection from the Conditionable trait.
@driesvints The expression causes a lot more issues when the initial value is not found, especially with firstWhere(), first() and more when the $callback is null. This has been in the when functionality since the beginning. This has to return $this so the builder eventually gets returned. |
Here's the repo |
I believe returning the whole collection for |
I have to say I am a bit disappointed. But at the very least I think it should be documented in the upgrade notes, as it's a breaking change in behaviour. |
Description:
When using
firstWhere()
inside awhen()
on a collection I don't get what I think I should.Steps To Reproduce:
On a brand new Laravel project, add the following Unit test
The second assertion fails, as
$notFoundUsingWhen
is actually the whole collection, rather thatnull
Note
The unit test passes in Laravel 8.83.25
The text was updated successfully, but these errors were encountered: