Illuminate\Support\Traits\Tappable을 use하면, 클래스에 tap() 메서드를 추가할 수 있습니다.
trait Tappable{ public function tap($callback = null) { return tap($this, $callback); }}
즉 인스턴스 메서드 버전의 tap()을 제공할 뿐입니다.
use Illuminate\Support\Traits\Tappable;class ReportBuilder{ use Tappable;}$builder = new ReportBuilder();$builder = $builder->tap(function (ReportBuilder $instance) { logger()->debug('builder initialized');});
Tappable은 Fluent API 도중에 부수 효과를 끼워 넣고 싶을 때 편리합니다. Macroable과 Conditionable을 조합하면, Laravel다운 확장 가능한 빌더를 만들 수 있습니다.
1
유창한 클래스를 만들기
use Illuminate\Support\Traits\Conditionable;use Illuminate\Support\Traits\Macroable;use Illuminate\Support\Traits\Tappable;class QueryPresetBuilder{ use Macroable; use Conditionable; use Tappable; protected array $filters = []; public function where(string $key, mixed $value): static { $this->filters[$key] = $value; return $this; } public function toArray(): array { return $this->filters; }}