简体   繁体   中英

How I can add an item to an array items of a column in Laravel?

this is data from the skills column of the resumes table:

{"en_title":"resume title in english","en":["item 1","item 2"]}

I want to update the en column in livewire. I can add an item to the en array but can't save or update the database. this is livewire Class:

    public Resume $resume;
    public $en;

    protected $rules = [
        'en' => 'string',
    ];

    public function save()
    {
        $this->validate();

    $items = $this->resume->skills['en'];
    $items[] = $this->en;
    $this->resume->update(['skills.en' => $items]);
    }

And View:

    <form wire:submit.prevent="save">
           <input type="text" wire:model.defer="en">
           <button type="submit">ADD</button>
    </form>

dd of $this->resume->skills['en'] is:

array:2 [▼
  0 => "item 1"
  1 => "item 2"
]

And dd of $items[] = $this->en is:

array:3 [▼
  0 => "item 1"
  1 => "item 2"
  2 => "new item"
]

but can't save in database by: $this->resume->update(['skills.en' => $items]); I think the skills.en name has problem but can't solve.

$this->resume->update([
  'skills' => [
    'en' => $items
  ],
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM