Skip to content

Commit

Permalink
Allow to define before() and after() callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
hschimpf committed Sep 26, 2023
1 parent a17683c commit 6b34fcd
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/ResourceRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ final public function __construct(
protected Request $request,
) {}

protected function before(Builder $query): void {}

final public function handle(Builder $query, Closure $next): Builder | Collection | LengthAwarePaginator {
$this->before($query);

// check if query param wasn't defined and just return
if (null !== $with = $this->request->query('with')) {
// convert to array if it is a coma separated string
Expand Down Expand Up @@ -86,9 +90,13 @@ final public function handle(Builder $query, Closure $next): Builder | Collectio
// append relation counts to the query
$query->withCount($this->with_count);

$this->after($query);

return $next($query);
}

protected function after(Builder $query): void {}

private function parseWiths(): void {
$with = [];
foreach ($this->with as $idx => $relation) {
Expand Down

0 comments on commit 6b34fcd

Please sign in to comment.