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"]
一個表單傳遞過來一個商品的資訊,並且這個商品
$data=$request->all(); $data['text']=mb_substr($data['content'],0,64); $save=Item::create($data);
商品建立成功,然後需要往圖片庫裡加圖片$data['itemimages'],
$save->itemimages()->save($data['itemimages']);//報錯$save->itemimages()->saveMany($data['itemimages']);//報錯$save->itemimages()->create($data['itemimages']);//item_id有值,image為空白
後來嘗試格式換一下
"itemimages" => array:1 [▼ "image" => "/editor/php/../attached/image/20151203/20151203071049_60053.png" ]$save->itemimages()->create($data['itemimages']);//成功
那麼多張圖片怎麼辦
Item已經定義
public function itemimages(){ return $this->hasMany("App\Itemimage");}
回複內容:
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"]
一個表單傳遞過來一個商品的資訊,並且這個商品
$data=$request->all(); $data['text']=mb_substr($data['content'],0,64); $save=Item::create($data);
商品建立成功,然後需要往圖片庫裡加圖片$data['itemimages'],
$save->itemimages()->save($data['itemimages']);//報錯$save->itemimages()->saveMany($data['itemimages']);//報錯$save->itemimages()->create($data['itemimages']);//item_id有值,image為空白
後來嘗試格式換一下
"itemimages" => array:1 [▼ "image" => "/editor/php/../attached/image/20151203/20151203071049_60053.png" ]$save->itemimages()->create($data['itemimages']);//成功
那麼多張圖片怎麼辦
Item已經定義
public function itemimages(){ return $this->hasMany("App\Itemimage");}
$itemimages=[];foreach($data['itemimages'] as $v){ $itemimages[]=new App\Itemimage(['image' => $v['image']),}$save->itemimages()->saveMany($itemimages);