Home > Backend Development > PHP Tutorial > laravel5一对多的create问题

laravel5一对多的create问题

WBOY
Release: 2016-06-06 20:23:23
Original
1700 people have browsed it

<code>array:7 [▼
  "_token" => "Y9szFJQYRcmHpyBB1McOE9tbUoZmshmJHzfV1hMG"
  "title" => "1"
  "image" => "2"
  "itemimages" => array:2 [▼
    0 => array:1 [▼
      "image" => "/editor/php/../attached/image/20151203/20151203071049_60053.png"
    ]
    1 => array:1 [▼
      "image" => "/editor/php/../attached/image/20151203/20151203071049_32261.png"
    ]
  ]
  "category_id" => "4"
  "content" => "3"
  "text" => "3"
]</code>
Copy after login
Copy after login

一个表单传递过来一个商品的信息,并且这个商品

<code>        $data=$request->all();
        $data['text']=mb_substr($data['content'],0,64);
        $save=Item::create($data);</code>
Copy after login
Copy after login

商品创建成功,然后需要往图片库里加图片$data['itemimages'],

<code>$save->itemimages()->save($data['itemimages']);//报错
$save->itemimages()->saveMany($data['itemimages']);//报错
$save->itemimages()->create($data['itemimages']);//item_id有值,image为空</code>
Copy after login
Copy after login

后来尝试格式换一下

<code>"itemimages" => array:1 [▼
      "image" => "/editor/php/../attached/image/20151203/20151203071049_60053.png"
  ]

$save->itemimages()->create($data['itemimages']);//成功</code>
Copy after login
Copy after login

那么多张图片怎么办

Item已经定义

<code>public function itemimages(){
    return $this->hasMany("App\Itemimage");
}</code>
Copy after login
Copy after login

回复内容:

<code>array:7 [▼
  "_token" => "Y9szFJQYRcmHpyBB1McOE9tbUoZmshmJHzfV1hMG"
  "title" => "1"
  "image" => "2"
  "itemimages" => array:2 [▼
    0 => array:1 [▼
      "image" => "/editor/php/../attached/image/20151203/20151203071049_60053.png"
    ]
    1 => array:1 [▼
      "image" => "/editor/php/../attached/image/20151203/20151203071049_32261.png"
    ]
  ]
  "category_id" => "4"
  "content" => "3"
  "text" => "3"
]</code>
Copy after login
Copy after login

一个表单传递过来一个商品的信息,并且这个商品

<code>        $data=$request->all();
        $data['text']=mb_substr($data['content'],0,64);
        $save=Item::create($data);</code>
Copy after login
Copy after login

商品创建成功,然后需要往图片库里加图片$data['itemimages'],

<code>$save->itemimages()->save($data['itemimages']);//报错
$save->itemimages()->saveMany($data['itemimages']);//报错
$save->itemimages()->create($data['itemimages']);//item_id有值,image为空</code>
Copy after login
Copy after login

后来尝试格式换一下

<code>"itemimages" => array:1 [▼
      "image" => "/editor/php/../attached/image/20151203/20151203071049_60053.png"
  ]

$save->itemimages()->create($data['itemimages']);//成功</code>
Copy after login
Copy after login

那么多张图片怎么办

Item已经定义

<code>public function itemimages(){
    return $this->hasMany("App\Itemimage");
}</code>
Copy after login
Copy after login

<code>$itemimages=[];
foreach($data['itemimages'] as $v){
    $itemimages[]=new App\Itemimage(['image' => $v['image']),
}

$save->itemimages()->saveMany($itemimages);</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template