Skip to content

Commit b3c1e34

Browse files
committed
fix: adding relationship support within the fields method
1 parent 8b3a268 commit b3c1e34

File tree

4 files changed

+114
-3
lines changed

4 files changed

+114
-3
lines changed

docs-v2/content/en/api/relations.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,46 @@ Restify handles all relationships and gives you an expressive way to list resour
1313

1414
## Definition
1515

16-
The list of relationships should be defined into a repository method called `related`:
16+
### Option 1: Using the `fields` method (Recommended)
17+
18+
You can define relationships directly within your `fields` method alongside regular fields. Restify will automatically detect eager fields and handle them as relationships:
19+
20+
```php
21+
public function fields(RestifyRequest $request): array
22+
{
23+
return [
24+
field('id')->readonly(),
25+
field('name')->storingRules('required'),
26+
field('email')->storingRules('required', 'unique:users'),
27+
28+
// Relationships - automatically detected and handled
29+
belongsToMany('roles', RoleRepository::class),
30+
hasMany('posts', PostRepository::class),
31+
belongsTo('company', CompanyRepository::class),
32+
33+
field('created_at')->readonly(),
34+
];
35+
}
36+
```
37+
38+
Using the helper functions:
39+
40+
```php
41+
belongsTo('relationship', RepositoryClass::class)
42+
belongsToMany('relationship', RepositoryClass::class)
43+
hasOne('relationship', RepositoryClass::class)
44+
hasMany('relationship', RepositoryClass::class)
45+
morphTo('relationship')
46+
morphOne('relationship', RepositoryClass::class)
47+
morphMany('relationship', RepositoryClass::class)
48+
morphToMany('relationship', RepositoryClass::class)
49+
morphedByMany('relationship', RepositoryClass::class)
50+
```
51+
52+
53+
### Option 2: Using the `related` method
54+
55+
Alternatively, you can define relationships using the traditional `related` method:
1756

1857
```php
1958
public static function related(): array

src/Repositories/Repository.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,14 @@ public function collectFields(RestifyRequest $request): FieldCollection
248248
$method = 'fieldsForUpdateBulk';
249249
}
250250

251+
$allFields = $this->filter($this->{$method}($request));
252+
253+
$regularFields = array_filter($allFields, function($field) {
254+
return !($field instanceof EagerField);
255+
});
256+
251257
return FieldCollection::make(
252-
array_values($this->filter($this->{$method}($request)))
258+
array_values($regularFields)
253259
)->merge(
254260
$this->extraFields($request)
255261
)->setRepository($this);

src/Traits/InteractWithSearch.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Binaryk\LaravelRestify\Traits;
44

55
use Binaryk\LaravelRestify\Eager\RelatedCollection;
6+
use Binaryk\LaravelRestify\Fields\EagerField;
67
use Binaryk\LaravelRestify\Filters\AdvancedFiltersCollection;
78
use Binaryk\LaravelRestify\Filters\Filter;
89
use Binaryk\LaravelRestify\Filters\MatchesCollection;
@@ -46,7 +47,22 @@ public static function include(): array
4647

4748
public static function collectRelated(): RelatedCollection
4849
{
49-
return RelatedCollection::make(static::include());
50+
$related = static::include();
51+
52+
if (empty($related)) {
53+
$instance = new static();
54+
$request = app(RestifyRequest::class);
55+
$fields = $instance->fields($request);
56+
57+
$eagerFields = collect($fields)
58+
->filter(fn($field) => $field instanceof EagerField)
59+
->mapWithKeys(fn($field) => [$field->attribute => $field])
60+
->toArray();
61+
62+
$related = $eagerFields;
63+
}
64+
65+
return RelatedCollection::make($related);
5066
}
5167

5268
public static function matches(): array

src/helpers.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,53 @@ function currentRepository(): Repository
7373
return app(RepositoryInstance::class)->current();
7474
}
7575
}
76+
77+
78+
if (! function_exists('belongsTo')) {
79+
function belongsTo(string $attribute, string $repository): Binaryk\LaravelRestify\Fields\BelongsTo
80+
{
81+
return Binaryk\LaravelRestify\Fields\BelongsTo::make($attribute, $repository);
82+
}
83+
}
84+
85+
if (! function_exists('belongsToMany')) {
86+
function belongsToMany(string $attribute, string $repository): Binaryk\LaravelRestify\Fields\BelongsToMany
87+
{
88+
return Binaryk\LaravelRestify\Fields\BelongsToMany::make($attribute, $repository);
89+
}
90+
}
91+
92+
if (! function_exists('hasOne')) {
93+
function hasOne(string $attribute, string $repository): Binaryk\LaravelRestify\Fields\HasOne
94+
{
95+
return Binaryk\LaravelRestify\Fields\HasOne::make($attribute, $repository);
96+
}
97+
}
98+
99+
if (! function_exists('hasMany')) {
100+
function hasMany(string $attribute, string $repository): Binaryk\LaravelRestify\Fields\HasMany
101+
{
102+
return Binaryk\LaravelRestify\Fields\HasMany::make($attribute, $repository);
103+
}
104+
}
105+
106+
if (! function_exists('morphOne')) {
107+
function morphOne(string $attribute, string $repository): Binaryk\LaravelRestify\Fields\MorphOne
108+
{
109+
return Binaryk\LaravelRestify\Fields\MorphOne::make($attribute, $repository);
110+
}
111+
}
112+
113+
if (! function_exists('morphMany')) {
114+
function morphMany(string $attribute, string $repository): Binaryk\LaravelRestify\Fields\MorphMany
115+
{
116+
return Binaryk\LaravelRestify\Fields\MorphMany::make($attribute, $repository);
117+
}
118+
}
119+
120+
if (! function_exists('morphToMany')) {
121+
function morphToMany(string $attribute, string $repository): Binaryk\LaravelRestify\Fields\MorphToMany
122+
{
123+
return Binaryk\LaravelRestify\Fields\MorphToMany::make($attribute, $repository);
124+
}
125+
}

0 commit comments

Comments
 (0)