Skip to content

Tap method issue #20419

@ed-fruty

Description

@ed-fruty
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions