0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

nuxt(javascript)からlaravelへapiで配列データを渡す

Posted at

やりたいこと

nuxt(javascript)からlaravelへapiで配列データを渡したい。

結論

jsからapiへ渡すときはこうなる

JavaScript
for (let i = 0; i < this.formData.length; i++) {
 formData.append('files[]', this.formData.files[i])
 formData.append('file_infos[' + i + '][title]', this..formData.files[i].title)
 formData.append('file_infos[' + i + '][outline]', this.formData.files[i].outline)
}

// これで
// [0] => array[
// title => ‘たいとる’,
// outline => ‘がいよう’;
// ]
// として受け取れる

// 下記はできそうでできない(なぜかnull)になる
// const array = [
//  title: this.formData[i].title
//  outline: this.formData[i].outline
// ]
// formData.append('file_infos[' + i + ']',ary)

解説

Object.keysと
some関数を使用する

ちなみにバリデーションでは

laravel
'file_infos.*.title'    => [
 'required',
 'string',
],
'file_infos.*.outline'    => [
 'nullable',
 'string',
]

のような記述になる。
laravel公式ドキュメントにも記載されています。
公式リンク
https://readouble.com/laravel/8.x/ja/validation.html

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?