- 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.
-
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).
-
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
Description:
I created pull request#20384 to fix
tapmethod problem, but maybe Taylor didn't understand what is the problem exactly. So i decided to create an issue and describe it here.The main problem is
tapmethod passesIlluminate\Database\Query\Builderto the callback, so it's impossible to use Eloquent Builder features in the callback (like relations, scopes).But
whenmethod works correctly. It's usesIlluminate\Database\Eloquent\Builderas argument for the callback.Based on this we have a new issue - we can't use one type hint callback for the
tapandwhenmethods.Steps To Reproduce: