Skip to content

Commit

Permalink
Adding MorphOne field.
Browse files Browse the repository at this point in the history
  • Loading branch information
binaryk committed Dec 21, 2020
1 parent ef46473 commit 511f5ff
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
20 changes: 20 additions & 0 deletions docs/docs/4.0/filtering/filtering.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,26 @@ public function foo() {
}
```

## Related eager

You can define an eager field to represent and serialize your data:

```php

use Binaryk\LaravelRestify\Fields\BelongsTo;
use Binaryk\LaravelRestify\Fields\MorphToMany;

public static function related(): array
{
return [
'user' => BelongsTo::make('user', 'user', UserRepository::class),
'comments' => MorphToMany::make('comments', 'comments', CommentRepository::class),
];
}
```

You have the following relations available: `MorphToMany`, `MorphOne` `BelongsTo`, `HasOne`, `HasMany`, `BelongsToMany`.

### Custom data format

You can use a custom related cast class (aka transformer). You can do so by modifying the `restify.casts.related`
Expand Down
7 changes: 7 additions & 0 deletions src/Fields/MorphOne.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Binaryk\LaravelRestify\Fields;

class MorphOne extends BelongsTo
{
}
81 changes: 81 additions & 0 deletions tests/Fields/MorphOneFieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Binaryk\LaravelRestify\Tests\Fields;

use Binaryk\LaravelRestify\Fields\BelongsTo;
use Binaryk\LaravelRestify\Fields\MorphOne;
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
use Binaryk\LaravelRestify\Repositories\Repository;
use Binaryk\LaravelRestify\Restify;
use Binaryk\LaravelRestify\Tests\Fixtures\Post\Post;
use Binaryk\LaravelRestify\Tests\Fixtures\User\User;
use Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository;
use Binaryk\LaravelRestify\Tests\IntegrationTest;

class MorphOneFieldTest extends IntegrationTest
{
protected function setUp(): void
{
parent::setUp();
$this->authenticate();

Restify::repositories([
PostWithMophOneRepository::class,
]);
}

protected function tearDown(): void
{
parent::tearDown();
Repository::clearResolvedInstances();
}

public function test_morph_one_present_on_show_when_specified_related()
{
$post = factory(Post::class)->create([
'user_id' => factory(User::class),
]);

$relationships = $this->get(PostWithMophOneRepository::uriKey() . "/$post->id?related=user")
->assertJsonStructure([
'data' => [
'relationships' => [
'user' => [
'id',
'type',
'attributes',
],
],
],
])
->json('data.relationships');

$this->assertNotNull($relationships);

$relationships = $this->get(PostWithMophOneRepository::uriKey() . "/$post->id")
->json('data.relationships');

$this->assertNull($relationships);
}
}

class PostWithMophOneRepository extends Repository
{
public static $model = Post::class;

public static function getRelated()
{
return [
'user' => BelongsTo::make('user', 'user', UserRepository::class),
];
}

public function fields(RestifyRequest $request)
{
return [
field('title'),

MorphOne::make('user', 'user', UserRepository::class),
];
}
}

0 comments on commit 511f5ff

Please sign in to comment.