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

Nova cannot update a table column with a space #6269

Closed
richardkeep opened this issue Mar 11, 2024 · 3 comments
Closed

Nova cannot update a table column with a space #6269

richardkeep opened this issue Mar 11, 2024 · 3 comments

Comments

@richardkeep
Copy link

richardkeep commented Mar 11, 2024

  • Laravel Version: 10.47.0
  • Nova Version: 4.35.16
  • PHP Version: 8.1
  • Database Driver & Version: MySQL 8.0
  • Operating System and Version: Mac OS
  • Browser type and version: Google Chrome

Description:

When you have a table column name with a space, Nova can display the value but you cannot update it.

Detailed steps to reproduce the issue on a fresh Nova installation:

  1. Create a Model, e.g Post model and its migration
Schema::create('posts', function (Blueprint $table) {
    $table->id();
    $table->string('Post Name')->nullable(); // a column with a space
}
  1. Migrate the table and manually add a record in DB. (maybe using tinker or TablePlus)

  2. Create a Nova Resource: App\Nova\Post

  3. Add the Field: Text::make('Post Name', 'Post Name')'

  4. Open the post edit page and try to update it and submit.

  5. The Post Name won't update in the database

  6. Change the table column from Post Name to something like post_name or postname and update the Nova field accordingly.

  7. Try to update the same post.

  8. It will update!!!

I have searched for a similar issue on the closed issues table and the discussion tab and it seems no one has ever encountered the same. I am working on a legacy project and I am not allowed to rename the table columns for now. I believe this is a bug.

@crynobone crynobone added pending Issues that are pending triage and removed pending Issues that are pending triage labels Mar 12, 2024
@crynobone
Copy link
Member

Check the issue with the Laravel Framework team and we have concluded that it's not a common usage and isn't being tested.

Since you can't change the database, it could be possible to create an observer to rename post_name to Post Name:

    /**
     * Handle the User "creating" event.
     */
    public function creating(Post $post): void
    {
        $post->setAttribute('Post Name', $post->post_name);
        unset($post->post_name);
    }

@crynobone crynobone closed this as not planned Won't fix, can't repro, duplicate, stale Mar 12, 2024
@crynobone
Copy link
Member

Alternatively you can set a accessor and mutator to handle this requirements.

@richardkeep
Copy link
Author

@crynobone Creating a mutator still cannot fix it

public function post_name(): Attribute
{
    return Attribute::make(
        get: fn () => $this->{'Post Name'}
    );
}

and in the Nova resource

Text::make('Post Name', 'post_name')->onlyOnForms(), // doesn't show the value from DB for editing
Text::make('Post Name', 'Post Name')->exceptOnForms(), // shows the value from DB

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

No branches or pull requests

2 participants