laravel5一对多的create问题
程序员文章站
2022-06-17 18:02:33
...
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);
推荐阅读
-
Bootstrap modal 多弹窗之叠加关闭阴影遮罩问题的解决方法
-
Bootstrap modal 多弹窗之叠加引起的滚动条遮罩阴影问题
-
微信JSSDK多图片上传并且解决IOS系统上传一直加载的问题
-
汉武帝颁布的推恩令到底有多厉害 诸侯王的问题解决了吗
-
Bootstrap modal 多弹窗之叠加显示不出弹窗问题的解决方案
-
Ubuntu18.0 解决python虚拟环境中不同用户下或者python多版本环境中指定虚拟环境的使用问题
-
JPA中实现双向一对多的关联关系
-
关于MyBatis 查询数据时属性中多对一的问题(多条数据对应一条数据)
-
Django 标签筛选的实现代码(一对多、多对多)
-
react-native 在新版Xcode(10+)中运行出现的问题: node_modules/react-native/third-party/glog-0.3.4 , C compiler cannot create executables