Skip to content

Commit b7e49c5

Browse files
committed
Use scoped queries by default
1 parent b48e3e4 commit b7e49c5

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/Http/Requests/InteractWithRepositories.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,23 @@ public function newQueryWithoutScopes($uriKey = null)
133133
return $this->viaQuery();
134134
}
135135

136+
/**
137+
* Get a new query builder for the underlying model.
138+
*
139+
* @param null $uriKey
140+
* @return \Illuminate\Database\Eloquent\Builder
141+
* @throws EntityNotFoundException
142+
* @throws UnauthorizedException
143+
*/
144+
public function newQuery($uriKey = null)
145+
{
146+
if (! $this->isViaRepository()) {
147+
return $this->model($uriKey)->newQuery();
148+
}
149+
150+
return $this->scopedViaQuery();
151+
}
152+
136153
/**
137154
* Get a new instance of the underlying model.
138155
*
@@ -159,7 +176,7 @@ public function model($uriKey = null)
159176
*/
160177
public function findModelQuery($repositoryId = null, $uriKey = null)
161178
{
162-
return $this->newQueryWithoutScopes($uriKey)->whereKey(
179+
return $this->newQuery($uriKey)->whereKey(
163180
$repositoryId ?? request('repositoryId')
164181
);
165182
}
@@ -196,11 +213,23 @@ public function viaParentModel()
196213
return once(fn () => $parent::newModel()->newQueryWithoutScopes()->whereKey($this->viaRepositoryId)->firstOrFail());
197214
}
198215

216+
public function scopedViaParentModel()
217+
{
218+
$parent = $this->repository($this->viaRepository);
219+
220+
return once(fn () => $parent::newModel()->newQuery()->whereKey($this->viaRepositoryId)->firstOrFail());
221+
}
222+
199223
public function viaQuery()
200224
{
201225
return $this->viaParentModel()->{$this->viaRelationship ?? request('repository')}();
202226
}
203227

228+
public function scopedViaQuery()
229+
{
230+
return $this->scopedViaParentModel()->{$this->viaRelationship ?? request('repository')}();
231+
}
232+
204233
/**
205234
* Get a new instance of the "related" resource being requested.
206235
*

0 commit comments

Comments
 (0)