Skip to content

Commit 511f5ff

Browse files
committed
Adding MorphOne field.
1 parent ef46473 commit 511f5ff

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

docs/docs/4.0/filtering/filtering.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,26 @@ public function foo() {
336336
}
337337
```
338338

339+
## Related eager
340+
341+
You can define an eager field to represent and serialize your data:
342+
343+
```php
344+
345+
use Binaryk\LaravelRestify\Fields\BelongsTo;
346+
use Binaryk\LaravelRestify\Fields\MorphToMany;
347+
348+
public static function related(): array
349+
{
350+
return [
351+
'user' => BelongsTo::make('user', 'user', UserRepository::class),
352+
'comments' => MorphToMany::make('comments', 'comments', CommentRepository::class),
353+
];
354+
}
355+
```
356+
357+
You have the following relations available: `MorphToMany`, `MorphOne` `BelongsTo`, `HasOne`, `HasMany`, `BelongsToMany`.
358+
339359
### Custom data format
340360

341361
You can use a custom related cast class (aka transformer). You can do so by modifying the `restify.casts.related`

src/Fields/MorphOne.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Fields;
4+
5+
class MorphOne extends BelongsTo
6+
{
7+
}

tests/Fields/MorphOneFieldTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Tests\Fields;
4+
5+
use Binaryk\LaravelRestify\Fields\BelongsTo;
6+
use Binaryk\LaravelRestify\Fields\MorphOne;
7+
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
8+
use Binaryk\LaravelRestify\Repositories\Repository;
9+
use Binaryk\LaravelRestify\Restify;
10+
use Binaryk\LaravelRestify\Tests\Fixtures\Post\Post;
11+
use Binaryk\LaravelRestify\Tests\Fixtures\User\User;
12+
use Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository;
13+
use Binaryk\LaravelRestify\Tests\IntegrationTest;
14+
15+
class MorphOneFieldTest extends IntegrationTest
16+
{
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
$this->authenticate();
21+
22+
Restify::repositories([
23+
PostWithMophOneRepository::class,
24+
]);
25+
}
26+
27+
protected function tearDown(): void
28+
{
29+
parent::tearDown();
30+
Repository::clearResolvedInstances();
31+
}
32+
33+
public function test_morph_one_present_on_show_when_specified_related()
34+
{
35+
$post = factory(Post::class)->create([
36+
'user_id' => factory(User::class),
37+
]);
38+
39+
$relationships = $this->get(PostWithMophOneRepository::uriKey() . "/$post->id?related=user")
40+
->assertJsonStructure([
41+
'data' => [
42+
'relationships' => [
43+
'user' => [
44+
'id',
45+
'type',
46+
'attributes',
47+
],
48+
],
49+
],
50+
])
51+
->json('data.relationships');
52+
53+
$this->assertNotNull($relationships);
54+
55+
$relationships = $this->get(PostWithMophOneRepository::uriKey() . "/$post->id")
56+
->json('data.relationships');
57+
58+
$this->assertNull($relationships);
59+
}
60+
}
61+
62+
class PostWithMophOneRepository extends Repository
63+
{
64+
public static $model = Post::class;
65+
66+
public static function getRelated()
67+
{
68+
return [
69+
'user' => BelongsTo::make('user', 'user', UserRepository::class),
70+
];
71+
}
72+
73+
public function fields(RestifyRequest $request)
74+
{
75+
return [
76+
field('title'),
77+
78+
MorphOne::make('user', 'user', UserRepository::class),
79+
];
80+
}
81+
}

0 commit comments

Comments
 (0)