Skip to content

Commit 48c7765

Browse files
committed
[Changed] Make model methods protected on resource query class
The model and modelOrFail methods were never meant to be used publicly on the resource query class - and were not documented as a result. To prevent them being used on this class (as it is confusing as to which model is returned), these methods are now public on the resource request, and protected on the query request. See #110
1 parent b8dafc6 commit 48c7765

File tree

4 files changed

+66
-30
lines changed

4 files changed

+66
-30
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ All notable changes to this project will be documented in this file. This projec
2323
- The type-hint of the first constructor argument has changed to `LaravelJsonApi\Core\Support\AppResolver`.
2424
- The deprecated `$container` property has been removed, and the `$app` property is now private. To access the
2525
current application instance in your server class, use `$this->app()` instead.
26+
- **BREAKING** [#110](https://github.com/laravel-json-api/laravel/issues/110) The `model()` and `modelOrFail()` methods
27+
on the `ResourceQuery` request class have been changed from `public` to `protected`. These were not documented for use
28+
on this query class, and were only intended to be used publicly on the `ResourceRequest` class. Although technically
29+
breaking, this change is unlikely to affect the vast majority of applications which should not be using the method.
2630

2731
## [1.1.0] - 2022-01-03
2832

src/Http/Requests/FormRequest.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -231,36 +231,6 @@ public function schema(): Schema
231231
return $this->jsonApi()->route()->schema();
232232
}
233233

234-
/**
235-
* Get the model that the request relates to, if the URL has a resource id.
236-
*
237-
* @return Model|object|null
238-
*/
239-
public function model(): ?object
240-
{
241-
$route = $this->jsonApi()->route();
242-
243-
if ($route->hasResourceId()) {
244-
return $route->model();
245-
}
246-
247-
return null;
248-
}
249-
250-
/**
251-
* Get the model that the request relates to, or fail if there is none.
252-
*
253-
* @return Model|object
254-
*/
255-
public function modelOrFail(): object
256-
{
257-
if ($model = $this->model()) {
258-
return $model;
259-
}
260-
261-
throw new LogicException('No model exists for this route.');
262-
}
263-
264234
/**
265235
* @return bool
266236
*/

src/Http/Requests/ResourceQuery.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
namespace LaravelJsonApi\Laravel\Http\Requests;
2121

2222
use Illuminate\Contracts\Validation\Validator;
23+
use Illuminate\Database\Eloquent\Model;
2324
use Illuminate\Http\Response;
2425
use LaravelJsonApi\Contracts\Auth\Authorizer;
2526
use LaravelJsonApi\Contracts\Query\QueryParameters;
@@ -28,6 +29,7 @@
2829
use LaravelJsonApi\Core\Query\FilterParameters;
2930
use LaravelJsonApi\Core\Query\IncludePaths;
3031
use LaravelJsonApi\Core\Query\SortFields;
32+
use LogicException;
3133
use Symfony\Component\HttpKernel\Exception\HttpException;
3234
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
3335
use function array_key_exists;
@@ -237,6 +239,36 @@ public function unrecognisedParameters(): array
237239
])->all();
238240
}
239241

242+
/**
243+
* Get the model that the request relates to, if the URL has a resource id.
244+
*
245+
* @return Model|object|null
246+
*/
247+
protected function model(): ?object
248+
{
249+
$route = $this->jsonApi()->route();
250+
251+
if ($route->hasResourceId()) {
252+
return $route->model();
253+
}
254+
255+
return null;
256+
}
257+
258+
/**
259+
* Get the model that the request relates to, or fail if there is none.
260+
*
261+
* @return Model|object
262+
*/
263+
protected function modelOrFail(): object
264+
{
265+
if ($model = $this->model()) {
266+
return $model;
267+
}
268+
269+
throw new LogicException('No model exists for this route.');
270+
}
271+
240272
/**
241273
* Get the default include paths to use if the client has provided none.
242274
*

src/Http/Requests/ResourceRequest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,36 @@ public static function forResourceIfExists(string $resourceType): ?ResourceReque
8686
return $resolver($resourceType, true);
8787
}
8888

89+
/**
90+
* Get the model that the request relates to, if the URL has a resource id.
91+
*
92+
* @return Model|object|null
93+
*/
94+
public function model(): ?object
95+
{
96+
$route = $this->jsonApi()->route();
97+
98+
if ($route->hasResourceId()) {
99+
return $route->model();
100+
}
101+
102+
return null;
103+
}
104+
105+
/**
106+
* Get the model that the request relates to, or fail if there is none.
107+
*
108+
* @return Model|object
109+
*/
110+
public function modelOrFail(): object
111+
{
112+
if ($model = $this->model()) {
113+
return $model;
114+
}
115+
116+
throw new LogicException('No model exists for this route.');
117+
}
118+
89119
/**
90120
* Perform resource authorization.
91121
*

0 commit comments

Comments
 (0)