首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将数据从一个表移动到另一个表?

如何将数据从一个表移动到另一个表?
EN

Stack Overflow用户
提问于 2019-05-17 00:58:41
回答 2查看 73关注 0票数 1

我在其他表中具有id的图像,并且我想将图像从id移动到其他表。我在变量$image_url中有逗号分隔的id,比如1,2 .I想要将id为1和2的图像移动到其他表中。我怎么能做到这一点?

代码语言:javascript
复制
  $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();
                }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-17 15:51:17

如果json对象中的数据像{"id":1},{"id":2},{"id":3},{"id":4}

代码语言:javascript
复制
$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();
}
票数 0
EN

Stack Overflow用户

发布于 2019-05-17 02:24:11

您需要在分隔的ID上重复,从存储它们的表中获取详细信息(在每个ID上),并将它们插入到目标表中。

代码语言:javascript
复制
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();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56173339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档