Skip to content

Fix/adding related helpers#629

Closed
binaryk wants to merge 2 commits into9.xfrom
fix/adding-related-helpers
Closed

Fix/adding related helpers#629
binaryk wants to merge 2 commits into9.xfrom
fix/adding-related-helpers

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()

@what-the-diff
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.

@binaryk binaryk closed this Aug 28, 2025
@binaryk binaryk deleted the fix/adding-related-helpers branch August 28, 2025 13:46
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