Skip to content

Commit 23399de

Browse files
committed
Merge branch 'release/1.5.0'
2 parents 991e66d + 117bc0c commit 23399de

File tree

22 files changed

+688
-13
lines changed

22 files changed

+688
-13
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@
22
All notable changes to this project will be documented in this file. This project adheres to
33
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
44

5+
## [1.5.0] - 2019-10-14
6+
7+
### Added
8+
- [#415](https://github.com/cloudcreativity/laravel-json-api/issues/415)
9+
Added a has-one-through JSON API resource relationship for the Eloquent has-one-through
10+
relationship that was added in Laravel 5.8.
11+
12+
### Fixed
13+
- [#439](https://github.com/cloudcreativity/laravel-json-api/issues/439)
14+
Fixed tests failing in Laravel `^6.1`. **Applications using Laravel 6 need to upgrade
15+
the JSON API testing package to `^2.0`** as follows:
16+
17+
```bash
18+
$ composer require --dev cloudcreativity/json-api-testing:^2.0
19+
```
20+
521
## [1.4.0] - 2019-09-04
622

723
### Added

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
},
4040
"require-dev": {
4141
"ext-sqlite3": "*",
42-
"cloudcreativity/json-api-testing": "^1.2",
42+
"cloudcreativity/json-api-testing": "^1.2|^2.0",
4343
"composer/semver": "^1.5",
4444
"guzzlehttp/guzzle": "^6.3",
4545
"mockery/mockery": "^1.1",

docs/basics/adapters.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ for Eloquent models. The relationship types available are `belongsTo`, `hasOne`,
224224
| Eloquent | JSON API |
225225
| :-- | :-- |
226226
| `hasOne` | `hasOne` |
227+
| `hasOneThrough` | `hasOneThrough` |
227228
| `belongsTo` | `belongsTo` |
228229
| `hasMany` | `hasMany` |
229230
| `belongsToMany` | `hasMany` |
@@ -338,15 +339,16 @@ class Adapter extends AbstractAdapter
338339
}
339340
```
340341

341-
#### Has-Many-Through
342+
#### Has-One-Through and Has-Many-Through
342343

343-
The JSON API `hasManyThrough` relation can be used for an Eloquent `hasManyThrough` relation. The important thing
344-
to note about this relationship is it is **read-only**. This is because the relationship can be modified in your API
345-
by modifying the intermediary model. For example, a `countries` resource might have many `posts` resources through
346-
an intermediate `users` resource. The relationship is effectively modified by creating and deleting posts and/or a
347-
user changing which country they are associated to.
344+
The JSON API `hasOneThrough` and `hasManyThrough` relations can be used for an Eloquent `hasOneThrough`
345+
and `hasManyThrough` relation. The important thing to note about these relationships is that both are **read-only**.
346+
This is because the relationship can be modified in your API by modifying the intermediary model.
347+
For example, a `countries` resource might have many `posts` resources through an intermediate `users` resource.
348+
The relationship is effectively modified by creating and deleting posts and/or a user changing which country they
349+
are associated to.
348350

349-
Define a has-many-through relationship on an adapter as follows:
351+
Use the `hasOneThrough()` or `hasManyThrough()` methods on your adapter as follows:
350352

351353
```php
352354
class Adapter extends AbstractAdapter

docs/installation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
Install using [Composer](http://getcomposer.org):
44

5-
``` bash
5+
```bash
66
$ composer require cloudcreativity/laravel-json-api
7-
$ composer require --dev cloudcreativity/json-api-testing
7+
$ composer require --dev "cloudcreativity/json-api-testing:^1.2|^2.0"
88
```
99

1010
This package's service provider and facade will be automatically added using package discovery. You will

src/Eloquent/AbstractAdapter.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,15 @@ protected function hasOne($modelKey = null)
555555
return new HasOne($modelKey ?: $this->guessRelation());
556556
}
557557

558+
/**
559+
* @param string|null $modelKey
560+
* @return HasOneThrough
561+
*/
562+
protected function hasOneThrough($modelKey = null)
563+
{
564+
return new HasOneThrough($modelKey ?: $this->guessRelation());
565+
}
566+
558567
/**
559568
* @param string|null $modelKey
560569
* @return HasMany

src/Eloquent/Concerns/SoftDeletesModels.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
use Carbon\Carbon;
2121
use CloudCreativity\LaravelJsonApi\Utils\Str;
22+
use Illuminate\Database\Eloquent\Builder;
2223
use Illuminate\Database\Eloquent\Model;
23-
use Illuminate\Database\Schema\Builder;
2424
use Illuminate\Support\Arr;
2525
use Illuminate\Support\Collection;
2626

src/Eloquent/HasOneThrough.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright 2019 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace CloudCreativity\LaravelJsonApi\Eloquent;
19+
20+
use CloudCreativity\LaravelJsonApi\Exceptions\RuntimeException;
21+
use Illuminate\Database\Eloquent\Relations;
22+
use Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface;
23+
24+
class HasOneThrough extends BelongsTo
25+
{
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
public function update($record, array $relationship, EncodingParametersInterface $parameters)
31+
{
32+
throw new RuntimeException('Modifying a has-one-through Eloquent relation is not supported.');
33+
}
34+
35+
/**
36+
* @inheritDoc
37+
*/
38+
public function replace($record, array $relationship, EncodingParametersInterface $parameters)
39+
{
40+
throw new RuntimeException('Modifying a has-one-through Eloquent relation is not supported.');
41+
}
42+
43+
/**
44+
* @inheritdoc
45+
*/
46+
protected function acceptRelation($relation)
47+
{
48+
return $relation instanceof Relations\HasOneThrough;
49+
}
50+
}

tests/dummy/app/History.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Copyright 2019 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace DummyApp;
19+
20+
use Illuminate\Database\Eloquent\Model;
21+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
22+
23+
class History extends Model
24+
{
25+
26+
/**
27+
* @var array
28+
*/
29+
protected $fillable = ['detail'];
30+
31+
/**
32+
* @return BelongsTo
33+
*/
34+
public function user(): BelongsTo
35+
{
36+
return $this->belongsTo(User::class);
37+
}
38+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright 2019 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace DummyApp\JsonApi\Histories;
19+
20+
use CloudCreativity\LaravelJsonApi\Eloquent\AbstractAdapter;
21+
use DummyApp\History;
22+
use Illuminate\Support\Collection;
23+
24+
class Adapter extends AbstractAdapter
25+
{
26+
27+
/**
28+
* Adapter constructor.
29+
*/
30+
public function __construct()
31+
{
32+
parent::__construct(new History());
33+
}
34+
35+
/**
36+
* @inheritDoc
37+
*/
38+
protected function filter($query, Collection $filters)
39+
{
40+
// TODO: Implement filter() method.
41+
}
42+
43+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright 2019 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace DummyApp\JsonApi\Histories;
19+
20+
use DummyApp\History;
21+
use Neomerx\JsonApi\Schema\SchemaProvider;
22+
23+
class Schema extends SchemaProvider
24+
{
25+
26+
/**
27+
* @var string
28+
*/
29+
protected $resourceType = 'histories';
30+
31+
/**
32+
* @param History $resource
33+
* @return string
34+
*/
35+
public function getId($resource)
36+
{
37+
return (string) $resource->getRouteKey();
38+
}
39+
40+
/**
41+
* @param History $resource
42+
* @return array
43+
*/
44+
public function getAttributes($resource)
45+
{
46+
return ['detail' => $resource->detail];
47+
}
48+
49+
/**
50+
* @param History $resource
51+
* @param bool $isPrimary
52+
* @param array $includeRelationships
53+
* @return array
54+
*/
55+
public function getRelationships($resource, $isPrimary, array $includeRelationships)
56+
{
57+
return [
58+
'user' => [
59+
self::SHOW_SELF => false,
60+
self::SHOW_RELATED => false,
61+
self::SHOW_DATA => isset($includeRelationships['user']),
62+
self::DATA => static function () use ($resource) {
63+
return $resource->user;
64+
},
65+
],
66+
];
67+
}
68+
69+
70+
}

0 commit comments

Comments
 (0)