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

Time:06-22

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.

CodePudding user response:

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