Skip to content

Fix/adding related helpers #629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 9.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion docs-v2/content/en/api/relations.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,46 @@ Restify handles all relationships and gives you an expressive way to list resour

## Definition

The list of relationships should be defined into a repository method called `related`:
### Option 1: Using the `fields` method (Recommended)

You can define relationships directly within your `fields` method alongside regular fields. Restify will automatically detect eager fields and handle them as relationships:

```php
public function fields(RestifyRequest $request): array
{
return [
field('id')->readonly(),
field('name')->storingRules('required'),
field('email')->storingRules('required', 'unique:users'),

// Relationships - automatically detected and handled
belongsToMany('roles', RoleRepository::class),
hasMany('posts', PostRepository::class),
belongsTo('company', CompanyRepository::class),

field('created_at')->readonly(),
];
}
```

Using the helper functions:

```php
belongsTo('relationship', RepositoryClass::class)
belongsToMany('relationship', RepositoryClass::class)
hasOne('relationship', RepositoryClass::class)
hasMany('relationship', RepositoryClass::class)
morphTo('relationship')
morphOne('relationship', RepositoryClass::class)
morphMany('relationship', RepositoryClass::class)
morphToMany('relationship', RepositoryClass::class)
morphedByMany('relationship', RepositoryClass::class)
```


### Option 2: Using the `related` method

Alternatively, you can define relationships using the traditional `related` method:

```php
public static function related(): array
Expand Down
17 changes: 8 additions & 9 deletions src/Exceptions/Solutions/OpenAiSolution.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@ public function __construct(protected Throwable $throwable)
now()->addHour(),
fn () => OpenAI::chat()->create([
'model' => config('restify.ai_solutions.model', 'gpt-4.1-mini'),
'messages' =>
'messages' => [
[
[
'role' => 'user',
'content' => $this->generatePrompt($this->throwable),
],
[
'role' => 'system',
'content' => 'Provide a concise solution to the problem described, tailored for a Laravel application using the Restify framework. Avoid explanations, examples, or code snippets. Respond with only the direct answer.',
],
'role' => 'user',
'content' => $this->generatePrompt($this->throwable),
],
[
'role' => 'system',
'content' => 'Provide a concise solution to the problem described, tailored for a Laravel application using the Restify framework. Avoid explanations, examples, or code snippets. Respond with only the direct answer.',
],
],
'max_tokens' => config('restify.ai_solutions.max_tokens', 1000),
'temperature' => 0,
])->choices[0]->message->content
Expand Down
8 changes: 7 additions & 1 deletion src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,14 @@ public function collectFields(RestifyRequest $request): FieldCollection
$method = 'fieldsForUpdateBulk';
}

$allFields = $this->filter($this->{$method}($request));

$regularFields = array_filter($allFields, function ($field) {
return ! ($field instanceof EagerField);
});

return FieldCollection::make(
array_values($this->filter($this->{$method}($request)))
array_values($regularFields)
)->merge(
$this->extraFields($request)
)->setRepository($this);
Expand Down
18 changes: 17 additions & 1 deletion src/Traits/InteractWithSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Binaryk\LaravelRestify\Traits;

use Binaryk\LaravelRestify\Eager\RelatedCollection;
use Binaryk\LaravelRestify\Fields\EagerField;
use Binaryk\LaravelRestify\Filters\AdvancedFiltersCollection;
use Binaryk\LaravelRestify\Filters\Filter;
use Binaryk\LaravelRestify\Filters\MatchesCollection;
Expand Down Expand Up @@ -46,7 +47,22 @@ public static function include(): array

public static function collectRelated(): RelatedCollection
{
return RelatedCollection::make(static::include());
$related = static::include();

if (empty($related)) {
$instance = new static;
$request = app(RestifyRequest::class);
$fields = $instance->fields($request);

$eagerFields = collect($fields)
->filter(fn ($field) => $field instanceof EagerField)
->mapWithKeys(fn ($field) => [$field->attribute => $field])
->toArray();

$related = $eagerFields;
}

return RelatedCollection::make($related);
}

public static function matches(): array
Expand Down
49 changes: 49 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,52 @@ function currentRepository(): Repository
return app(RepositoryInstance::class)->current();
}
}

if (! function_exists('belongsTo')) {
function belongsTo(string $attribute, string $repository): Binaryk\LaravelRestify\Fields\BelongsTo
{
return Binaryk\LaravelRestify\Fields\BelongsTo::make($attribute, $repository);
}
}

if (! function_exists('belongsToMany')) {
function belongsToMany(string $attribute, string $repository): Binaryk\LaravelRestify\Fields\BelongsToMany
{
return Binaryk\LaravelRestify\Fields\BelongsToMany::make($attribute, $repository);
}
}

if (! function_exists('hasOne')) {
function hasOne(string $attribute, string $repository): Binaryk\LaravelRestify\Fields\HasOne
{
return Binaryk\LaravelRestify\Fields\HasOne::make($attribute, $repository);
}
}

if (! function_exists('hasMany')) {
function hasMany(string $attribute, string $repository): Binaryk\LaravelRestify\Fields\HasMany
{
return Binaryk\LaravelRestify\Fields\HasMany::make($attribute, $repository);
}
}

if (! function_exists('morphOne')) {
function morphOne(string $attribute, string $repository): Binaryk\LaravelRestify\Fields\MorphOne
{
return Binaryk\LaravelRestify\Fields\MorphOne::make($attribute, $repository);
}
}

if (! function_exists('morphMany')) {
function morphMany(string $attribute, string $repository): Binaryk\LaravelRestify\Fields\MorphMany
{
return Binaryk\LaravelRestify\Fields\MorphMany::make($attribute, $repository);
}
}

if (! function_exists('morphToMany')) {
function morphToMany(string $attribute, string $repository): Binaryk\LaravelRestify\Fields\MorphToMany
{
return Binaryk\LaravelRestify\Fields\MorphToMany::make($attribute, $repository);
}
}
Loading