8000 Add relationship docs (#74) · martynling/laravel-json-api@2cab829 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2cab829

Browse files
GIANTCRABlindyhopchris
authored andcommitted
Add relationship docs (cloudcreativity#74)
* Fix password checking * Add read related resource & read relationship * Remove old custom controller docs
1 parent b015c8a commit 2cab829

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

docs/custom/controllers.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,44 @@ public function delete(RequestInterface $request)
144144
return $this->reply()->noContent();
145145
}
146146
```
147+
148+
## Relationship
149+
If you have defined relationships in the schema of a resource, you would then need to state the following in your controller too:
150+
151+
1. `readRelatedResource()`
152+
2. `readRelationship()`
153+
154+
### Read Related Resource
155+
If you link one resource to another through relationship, you'll need this to read the related resource.
156+
157+
```php
158+
/**
159+
* @param JsonApiRequest $request
160+
* @return mixed
161+
*/
162+
public function readRelatedResource(JsonApiRequest $request)
163+
{
164+
$model = $request->getRecord();
165+
$key = $request->getRelationshipName();
166+
return $this
167+
->reply()
168+
->content($model->{$key});
169+
}
170+
```
171+
172+
### Read Relationship
173+
This is for reading the relationship between two resources.
174+
```php
175+
/**
176+
* @param JsonApiRequest $request
177+
* @return mixed
178+
*/
179+
public function readRelationship(JsonApiRequest $request)
180+
{
181+
$model = $request->getRecord();
182+
$key = $request->getRelationshipName();
183+
return $this
184+
->reply()
185+
->relationship($model->{$key});
186+
}
187+
```

0 commit comments

Comments
 (0)
0