-
Notifications
You must be signed in to change notification settings - Fork 11k
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
[10.x] Add methods dumpWhen
and ddWhen
to Str and Collection
#48281
Conversation
Why need add a condition for $collect = collect([1,2,3])->toArray();
if (...) {
dd();
} |
Let's say we have n depth array of objects. While we are traversing the whole data, it would be useful to stop and check data if condition is met or not. Debbuging will be much easier this way, because we are focusing the only needed part of big data. It can be in any level don't just think about root collection. $hotels = collect([
collect(['id' => 3, 'name' => 'Sheraton', 'rating' => null]),
collect(['id' => 4, 'name' => 'Hilton', 'rating' => 3])
]);
$ratings = $hotels->map(
fn ($hotel) => $hotel->only('id', 'rating')
->ddWhen(is_null($hotel->get('rating')))
); This way, I can print only the hotel that have problem. |
So, Do you think this method is helpful for most of the projects!! |
The Wouldn't this work the same? $hotels = collect([
collect(['id' => 3, 'name' => 'Sheraton', 'rating' => null]),
collect(['id' => 4, 'name' => 'Hilton', 'rating' => 3]),
]);
$ratings = $hotels->map(fn ($hotel) => $hotel->when(
is_null($hotel->get('rating')),
$hotel->dd(),
fn () => $hotel->only('id', 'rating'),
)); Although, as it is meant to be used while debugging, I would personally switch to a regular closure and use an Most IDE's have shortcuts for converting between regular closures and arrow functions. Lastly, I know Laravel is ok with adding features on minor releases, but I am not sure if that extends to adding methods to interfaces/contracts. Maybe, if maintainers think this is a good addition to the framework, you might need to add these methods to the base classes first, and then send a PR to add it to the But I am not totally sure about this. |
Or even: $hotels = collect([
collect(['id' => 3, 'name' => 'Sheraton', 'rating' => null]),
collect(['id' => 4, 'name' => 'Hilton', 'rating' => 3]),
]);
$ratings = $hotels
->map(fn ($hotel) => $hotel->only('id', 'rating'))
->transform(fn ($hotel) => $hotel->when(is_null($hotel->get('ratings')), fn () => $hotel->dd())); Or terser: $hotels = collect([
collect(['id' => 3, 'name' => 'Sheraton', 'rating' => null]),
collect(['id' => 4, 'name' => 'Hilton', 'rating' => 3]),
]);
$ratings = $hotels
->map(fn ($hotel) => $hotel->only('id', 'rating'))
->transform(fn ($hotel) => $hotel->get('ratings') ?? $hotel->dd()); |
As you can see from the name of the method, this is just a shortcut of |
Sure, I get it, I just miss the value of adding those. Why not add convenience methods like Maybe there is an opportunity for a high order method, something like I guess this could be a better addition, in case you are interested to explore it. Well, at the end, let's wait for the maintainer's opinion on this. Have a nice day =) |
Thanks for your pull request to Laravel! Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include. If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions! If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response. |
@monurakkaya actually the It allows one to do this: $hotels = collect([
collect(['id' => 3, 'name' => 'Sheraton', 'rating' => null]),
collect(['id' => 4, 'name' => 'Hilton', 'rating' => 3]),
]);
$ratings = $hotels->map(
fn ($hotel) => $hotel->only('id', 'rating')
->when(\is_null($hotel->get('rating')))->dd()
); I thought you'd like to know, as it seems very handy for debugging and somewhat similar to your proposal. |
I believe it would be useful to have such a shortcut for dumping things especially when working in large nested arrays/objects.
usage: