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

[8.x] Allow tap() on Paginator #37682

Merged
merged 1 commit into from
Jun 14, 2021
Merged

Conversation

crynobone
Copy link
Member

@crynobone crynobone commented Jun 14, 2021

Before

$posts = App\Models\Post::search('Laravel')->paginate();

$posts->load('author');

return $posts;

After

return App\Models\Post::search('Laravel')
    ->paginate()
    ->tap(function ($posts) {
        $posts->load('author');
    });

Signed-off-by: Mior Muhammad Zaki [email protected]

Before

```php
$posts = App\Models\Post::search('Laravel')->paginate();

$posts->load('user');

return $posts;
```

After

```php
return App\Models\Post::search('Laravel')
    ->paginate()
    ->tap(fn ($posts) => $posts->load('author'));
```

Signed-off-by: Mior Muhammad Zaki <[email protected]>
@crynobone
Copy link
Member Author

Could be useful to add Illuminate\Support\Traits\Conditionable too

@taylorotwell taylorotwell merged commit 1472e3c into laravel:8.x Jun 14, 2021
@crynobone crynobone deleted the tappable-paginator branch June 14, 2021 14:13
@dabernathy89
Copy link

I believe this should be considered a breaking change, since tap would previously be forwarded to the underlying collection of the paginator.

@driesvints
Copy link
Member

Ping @crynobone ^

@crynobone
Copy link
Member Author

It would only be breaking change if you doing something like:

$users = User::paginate()->tap(function ($users) { /* ... */ });
$user = User::paginate();

$users->tap(function ($users) { /* ... */ }); // should behave the same

@crynobone
Copy link
Member Author

In most use-case you would generally interact with the collection using:

$users = User::paginate();

$user->setCollection($users->getCollection()->each(/* ... */));

@dabernathy89
Copy link

I'm sure it's not common, but regardless, if someone is using tap to access the underlying collection then this is a breaking change for them.

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.

4 participants