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

Tap method issue #20419

Closed
ed-fruty opened this issue Aug 4, 2017 · 1 comment
Closed

Tap method issue #20419

ed-fruty opened this issue Aug 4, 2017 · 1 comment

Comments

@ed-fruty
Copy link
Contributor

ed-fruty commented Aug 4, 2017

  • Laravel Version: 5.4.30
  • PHP Version: 7.1

Description:

I created pull request#20384 to fix tap method problem, but maybe Taylor didn't understand what is the problem exactly. So i decided to create an issue and describe it here.

  1. The main problem is tap method passes Illuminate\Database\Query\Builder to the callback, so it's impossible to use Eloquent Builder features in the callback (like relations, scopes).

  2. But when method works correctly. It's uses Illuminate\Database\Eloquent\Builder as argument for the callback.
    Based on this we have a new issue - we can't use one type hint callback for the tap and when methods.

Steps To Reproduce:

Model::tap(function ($builder) {
   /*
    * This will throw an exception
    * BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::with()'
    */
   $builer->with('relation');

});
// We can't use typehint for $builder argument
$callback = function ($builder) {
   echo get_class($builder);
};

Model::where('condition', 'value')
 ->when(true, $callback)  // Illuminate\Database\Eloquent\Builder
 ->tap($callback); // Illuminate\Database\Query\Builder
@themsaid
Copy link
Member

themsaid commented Aug 4, 2017

The PR was re-opened, thanks for follow up :)

@themsaid themsaid closed this as completed Aug 4, 2017
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

No branches or pull requests

2 participants