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

Fix/adding related helpers #629

wants to merge 2 commits into from

Conversation

binaryk
Copy link
Collaborator

@binaryk binaryk commented May 26, 2025

Added:

🔗 Relationship Helper Functions Directly Within the fields method

public function fields(RestifyRequest $request): array
{
    return [
        field('name')->storingRules('required'),
        field('email')->storingRules('required', 'unique:users'),
        
        // Clean, expressive relationship definitions
        belongsToMany('roles', RoleRepository::class),
        hasMany('posts', PostRepository::class),
        belongsTo('company', CompanyRepository::class),
        morphTo('commentable'),
    ];
}

Available helpers:

belongsTo()
belongsToMany()
hasOne()
hasMany()
morphTo()
morphOne()
morphMany()
morphToMany()
morphedByMany()

Copy link

what-the-diff bot commented May 26, 2025

PR Summary

  • Enhancement of API Documentation: The instructions on defining relationships have been updated in the relations.md to provide thorough clarity. Now, it includes two ways of defining the relationships, both by using the fields method as well as the conventional related method.

  • Inclusion of Example Codes: To provide a better understanding of defining relationships, code snippets for belongsTo, hasMany, and others have been added alongside their use with the fields method.

  • Redesign of OpenAiSolution.php: The structure of the messages array has gone through a revamp, which led to the elimination of redundant array nesting, making it much more uncomplicated and cleaner to decipher.

  • Improvements in collectFields Method: The method in Repository.php has seen enhancement with the introduction of a new variable $regularFields. This additional variable helps filter the eager fields before moving towards the return step with the FieldCollection.

  • Upgrades in collectRelated Method: The method in InteractWithSearch.php has been amplified for functionality. Now, if there are no predefined relationships, it checks and initiates the related fields.

  • New Helper Functions Added: The file helpers.php now includes several helper functions to assist in defining relationship fields like belongsTo, belongsToMany, hasOne, and hasMany. Moreover, the system checks for their existence and creates them only if they do not exist previously.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants