我在其他表中具有id的图像,并且我想将图像从id移动到其他表。我在变量$image_url中有逗号分隔的id,比如1,2 .I想要将id为1和2的图像移动到其他表中。我怎么能做到这一点?
$image_url = $request->input('image_id');
$separate_ids = explode(",", $image_url);
foreach ($separate_ids as $ids) {
$result_url['url'] = TempImage::where('id', '=', $separate_ids)
->get(['image']);
}
if ($result_url) {
$imageModel = new Image();
$imageModel->user_id = $userID;
$imageModel->user_type = $userType;
$imageModel->image = $result_url;
$result = $imageModel->save();
}发布于 2019-05-17 15:51:17
如果json对象中的数据像{"id":1},{"id":2},{"id":3},{"id":4}
$separate_ids = '[{"id":1},{"id":2},{"id":3},{"id":4}]';
$separate_ids_array = json_decode($separate_ids);
foreach($separate_ids_array as $separate_id)
{
// Get the URL according to the image ID
$url = TempImage::where('id', $separate_id->id)->first()->image;
// Store the image
$imageModel = new Image();
$imageModel->user_id = $userID;
$imageModel->user_type = $userType;
$imageModel->image = $url;
$imageModel->save();
}发布于 2019-05-17 02:24:11
您需要在分隔的ID上重复,从存储它们的表中获取详细信息(在每个ID上),并将它们插入到目标表中。
foreach($separate_ids as $id)
{
// Get the URL according to the image ID
$url = TempImage::where('id', $id)->first()->image;
// Store the image
$imageModel = new Image();
$imageModel->user_id = $userID;
$imageModel->user_type = $userType;
$imageModel->image = $url;
$imageModel->save();
}https://stackoverflow.com/questions/56173339
复制相似问题