我在其他具有ID的表中有图像,我想将图像从ID移至其他表。我在变量$ image_url中有逗号分隔的ID,例如1,2。我想将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();
}
答案 0 :(得分:1)
您需要重申分离的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();
}
答案 1 :(得分:1)
如果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();
}