Skip to content
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

Fix exists rule for foreign key column indicating table name #722

Conversation

nicodevs
Copy link
Contributor

Issue Description

On a clean installation, a foreign key column specifying the name of the related table generates an incorrect exists rule in the form request. For instance, the following YAML:

models:
  Message:
    subject: string
    sender_id: id foreign:users
controllers:
  Message:
    resource: api

... generates these rules in MessageStoreRequest and MessageUpdateRequest:

    public function rules(): array
    {
        return [
            'subject' => ['required', 'string'],
            'sender_id' => ['required', 'integer', 'exists:senders,id'],
        ];
    }

The expected rule for sender_id should be:

-'sender_id' => ['required', 'integer', 'exists:senders,id'],
+'sender_id' => ['required', 'integer', 'exists:users,id'],

Solution

This PR updates the Rules translator to verify if the column includes a foreign table modifier and applies it in the exists rule when present. A test has been added to ensure this behavior.

Please let me know if the code and test look good and matches the coding style. Thanks!

@jasonmccreary jasonmccreary merged commit e4a7397 into laravel-shift:master Dec 27, 2024
1 check passed
@jasonmccreary
Copy link
Collaborator

Thanks.

@nicodevs nicodevs deleted the fix-exists-rule-for-foreign-keys-with-table-name branch January 5, 2025 12:44
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