-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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] Adds firstOrFail to Illuminate\Support\Collections and Illuminate\Support\LazyCollections #38420
Conversation
Not sure why the checks were cancelled. I think I submitted the style fixes too early |
Are we OK that I mean, it kinda does makes sense, but the inconsistency can catch people by surprise. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also needs a test ensuring it's actually lazy, akin to the lazy test for the Sole
method.
Per Joseph Silber's suggestion Co-authored-by: Joseph Silber <contact@josephsilber.com>
Per Joseph Silber's suggestion Co-authored-by: Joseph Silber <contact@josephsilber.com>
Thanks @JosephSilber , I'm not so familiar with Lazy Collections, I'll look further later and try and get that test in you suggest. |
Whoops. Seems like Guess we should just revert it back to Sorry. |
Thought I was going mad! I'll revert tonight, but I'll add the tests. Thanks for your feedback. |
Hi @JosephSilber I've added the test as requested. If I'm honest, I don't understand Lazy Collections, so I can promise I've made the tests pass, but I can't tell you that the test is correct, and I value your feedback |
try { | ||
$collection->firstOrFail(); | ||
} catch (ItemNotFoundException $e) { | ||
// | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you expect this to throw? Why?
try { | ||
$collection->firstOrFail(function ($item) { | ||
return $item % 2 === 0; | ||
}); | ||
} catch (ItemNotFoundException $e) { | ||
// | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here. Why would this fail?
And really, you're not testing an actual failure, and ensuring it enumerates the whole collection once.
…ail enumerates the full collection
Further changes made. I hope I have Managed to match your suggestions @JosephSilber |
@@ -2,6 +2,7 @@ | |||
|
|||
namespace Illuminate\Tests\Support; | |||
|
|||
use Illuminate\Collections\ItemNotFoundException; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #38449
Nice work @powellblyth 👍 LGTM now. Only outstanding question is about the inconsistency of the HTTP response. I honestly don't know what the right answer should be here. |
Thank you. Not sure who can help about the HTTP aspect though. However, since it's not necessarily a resource, maybe the exception is ok, as you should only use this when you are expecting it. I do understand where it could confuse though. I'm not familiar with the framework enough to even contemplate, but could the system that shows a 404 not be altered to catch both ModelNotFound as well as ItemNotFound? . I'm just throwing that one out there. |
Please rebase and mark as ready. |
Further to this, if you have this in a http controller, you also get a 500
so it's an existing inconsistency.... |
@driesvints I've re-based |
So that's not an inconsistency at all. |
…te\Support\LazyCollections (laravel#38420) * Add firstOrFail method to Collections and LazyCollections * Restore test that got moved, fixed some grammaticals in test names * Style fixes * Make take(2) as take(1) instead * use unless instead of when Per Joseph Silber's suggestion Co-authored-by: Joseph Silber <contact@josephsilber.com> * Use Unless instead of When Per Joseph Silber's suggestion Co-authored-by: Joseph Silber <contact@josephsilber.com> * Revert L9 changes, add test requested by @JosephSilber * Remove unneeded try catch * Remove unneeded include * Test that when an enumeration does not contain an item, that firstOrFail enumerates the full collection * Style fix * Update Collection.php * Update LazyCollection.php Co-authored-by: Toby Powell-Blyth <tobypowell-blyth@elasticstage.com> Co-authored-by: Joseph Silber <contact@josephsilber.com> Co-authored-by: Taylor Otwell <taylor@laravel.com>
…te\Support\LazyCollections (laravel#38420) * Add firstOrFail method to Collections and LazyCollections * Restore test that got moved, fixed some grammaticals in test names * Style fixes * Make take(2) as take(1) instead * use unless instead of when Per Joseph Silber's suggestion Co-authored-by: Joseph Silber <contact@josephsilber.com> * Use Unless instead of When Per Joseph Silber's suggestion Co-authored-by: Joseph Silber <contact@josephsilber.com> * Revert L9 changes, add test requested by @JosephSilber * Remove unneeded try catch * Remove unneeded include * Test that when an enumeration does not contain an item, that firstOrFail enumerates the full collection * Style fix * Update Collection.php * Update LazyCollection.php Co-authored-by: Toby Powell-Blyth <tobypowell-blyth@elasticstage.com> Co-authored-by: Joseph Silber <contact@josephsilber.com> Co-authored-by: Taylor Otwell <taylor@laravel.com>
…te\Support\LazyCollections (laravel#38420) * Add firstOrFail method to Collections and LazyCollections * Restore test that got moved, fixed some grammaticals in test names * Style fixes * Make take(2) as take(1) instead * use unless instead of when Per Joseph Silber's suggestion Co-authored-by: Joseph Silber <contact@josephsilber.com> * Use Unless instead of When Per Joseph Silber's suggestion Co-authored-by: Joseph Silber <contact@josephsilber.com> * Revert L9 changes, add test requested by @JosephSilber * Remove unneeded try catch * Remove unneeded include * Test that when an enumeration does not contain an item, that firstOrFail enumerates the full collection * Style fix * Update Collection.php * Update LazyCollection.php Co-authored-by: Toby Powell-Blyth <tobypowell-blyth@elasticstage.com> Co-authored-by: Joseph Silber <contact@josephsilber.com> Co-authored-by: Taylor Otwell <taylor@laravel.com>
This PR recreates the FirstOrFail methods from Eloquent collections into the Illuminate Support Collection and LazyCollection, which makes writing code a little more fluent
e.g.
OLD
NEW