Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

SNaRe's avatar
Level 7

Edit page gives 500 error when hiddenOn('create')

I have a form that I want to add "races" through this form. I wanted to hide "races" when creating since "races" can be created after creating an "event". Relationship -> race belongs to one event. Event has many races.

            Forms\Components\Select::make('races')
                ->relationship('races', 'name')
                ->searchable()
                ->preload()
                ->editOptionForm(fn(Form $form) => Race::getForm())
                ->required()
                ->hiddenOn('create'),

I don't get error when I create a "race" ( apparently since it is hidden ), but I get it when editing it. How can I solve this? This is the error that I get

Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::getOwnerKeyName() {"userId":1,"exception":"[object] (BadMethodCallException(code: 0): Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::getOwnerKeyName() at /Users/xx/Code/race/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:67)

0 likes
1 reply
LaryAI's avatar
Level 58

It seems like you're encountering an issue with the relationship method on the Select component when trying to edit an "event" that has associated "races". The error message indicates that there's a call to an undefined method getOwnerKeyName() on a HasMany relationship, which suggests that there might be a problem with how the relationship is being handled or accessed.

Here's a potential solution to address the issue:

  1. Ensure that the races relationship is defined correctly in your Event model. It should return an instance of a relationship, such as BelongsTo, HasMany, BelongsToMany, etc.

  2. Since you want to hide the races field on the create form, but not on the edit form, you've correctly used hiddenOn('create'). However, the error seems to be related to the relationship method, not the visibility.

  3. Check if the Race::getForm() method is correctly implemented and does not cause any issues when called during the edit operation.

  4. If the Race::getForm() method is trying to access the getOwnerKeyName() method on a HasMany relationship, you might need to review the implementation of Race::getForm() to ensure it's compatible with the HasMany relationship.

  5. If the issue persists, you might want to temporarily remove the relationship, createOptionForm, and editOptionForm methods to see if the error goes away. This can help you isolate the problem.

  6. Lastly, ensure that all other parts of your form are correctly set up and that there are no other underlying issues that might be causing the error.

Here's a simplified version of your Select component without the potentially problematic methods:

Forms\Components\Select::make('races')
    ->searchable()
    ->preload()
    ->required()
    ->hiddenOn('create'),

If removing the relationship, createOptionForm, and editOptionForm methods resolves the error, you'll need to carefully reintroduce them one by one, checking for errors at each step to pinpoint the exact cause.

Remember to check your Event and Race models to ensure that the relationships are defined correctly and that there are no issues with the methods being called on those relationships. If you need further assistance, please provide more details about the Race::getForm() method and the relationship definitions in your models.

Please or to participate in this conversation.