Skip to content

Commit 1eaa9b8

Browse files
committed
[Feature] Make related models accessible on resource request
Adds the `toMany` and `toOne` methods to the resource request class. For modify relationship requests, this makes the models referenced in the request JSON accessible. Closes #110
1 parent a9e069e commit 1eaa9b8

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
All notable changes to this project will be documented in this file. This project adheres to
44
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
55

6+
## Unreleased
7+
8+
### Added
9+
10+
- [#110](https://github.com/laravel-json-api/laravel/issues/110) For requests that modify a relationship, it is now
11+
possible to get the model or models referenced in the request JSON using the `toOne()` or `toMany()` methods on the
12+
resource request class.
13+
614
## [2.0.0] - 2022-02-09
715

816
### Added

src/Http/Requests/ResourceRequest.php

+57
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
use Illuminate\Contracts\Validation\Validator;
2424
use Illuminate\Database\Eloquent\Model;
2525
use Illuminate\Http\Response;
26+
use Illuminate\Support\Collection;
2627
use LaravelJsonApi\Contracts\Auth\Authorizer;
2728
use LaravelJsonApi\Contracts\Schema\Relation;
2829
use LaravelJsonApi\Core\Document\ResourceObject;
2930
use LaravelJsonApi\Core\Exceptions\JsonApiException;
3031
use LaravelJsonApi\Core\Query\IncludePaths;
32+
use LaravelJsonApi\Core\Store\LazyRelation;
3133
use LaravelJsonApi\Core\Support\Str;
3234
use LaravelJsonApi\Spec\RelationBuilder;
3335
use LaravelJsonApi\Spec\ResourceBuilder;
@@ -49,6 +51,11 @@ class ResourceRequest extends FormRequest
4951
*/
5052
private ?array $validationData = null;
5153

54+
/**
55+
* @var LazyRelation|null
56+
*/
57+
private ?LazyRelation $relation = null;
58+
5259
/**
5360
* Specify the callback to use to guess the request class for a JSON API resource.
5461
*
@@ -116,6 +123,38 @@ public function modelOrFail(): object
116123
throw new LogicException('No model exists for this route.');
117124
}
118125

126+
/**
127+
* Get the model referenced in a to-one relationship.
128+
*
129+
* @return Model|object|null
130+
*/
131+
public function toOne(): ?object
132+
{
133+
if ($this->isModifyingRelationship()) {
134+
return $this->relation()->get();
135+
}
136+
137+
throw new LogicException(
138+
'Can only retrieve the model for a to-one relationship when the relationship is being modified.'
139+
);
140+
}
141+
142+
/**
143+
* Get the models referenced in a to-many relationship.
144+
*
145+
* @return Collection
146+
*/
147+
public function toMany(): Collection
148+
{
149+
if ($this->isModifyingRelationship()) {
150+
return $this->relation()->collect();
151+
}
152+
153+
throw new LogicException(
154+
'Can only retrieve the models for a to-many relationship when the relationship is being modified.'
155+
);
156+
}
157+
119158
/**
120159
* Perform resource authorization.
121160
*
@@ -557,4 +596,22 @@ private function validateRelationshipDocument(): void
557596
}
558597
}
559598

599+
/**
600+
* @return LazyRelation
601+
*/
602+
private function relation(): LazyRelation
603+
{
604+
if ($this->relation) {
605+
return $this->relation;
606+
}
607+
608+
$jsonApi = $this->jsonApi();
609+
610+
return $this->relation = new LazyRelation(
611+
$jsonApi->server(),
612+
$jsonApi->route()->relation(),
613+
$this->json()->all(),
614+
);
615+
}
616+
560617
}

0 commit comments

Comments
 (0)