如何解决Laravel中的多个图像编辑/更新问题?

我在数据库中提交了多张图像,现在我要编辑数据库中的所有图像,我更新了查询,但无法在数据库中上传图像。有3种模型:站点地图图像模式,位置地图图像模式,平面图图像模型,属性图像模型以及每个模型都与属性表相关,我已经编写了属性更新代码,但是在更新控制器时出现了一些错误,请检查我的代码并让我知道我在哪里在我的代码中犯了错误。 这是我的更新代码... UpdateController.php ..

public function update(Request $r,$property)
{

$validator = Validator::make($r->all(),[
    'title'                 => 'required|unique:properties,title,'.$property.',propId|max:255',// 'slug'                  => 'required|unique:properties,slug,propId|max:100','catId'                 => 'required|array|min:1',]);

if ($validator->fails()){
    return response()->json([$validator->errors()],302);
}else{

    $type = 'property';
    $chr  = substr($r->title,1);

    if (!is_dir(public_path().'/'.$type)) {
        $blogDir = public_path().'/'.$type;
        mkdir ($blogDir,0777,true);
    }
    if (!is_dir(public_path().'/'.$type.'/'.date("Ymd"))) {
        $dateDir = public_path().'/'.$type.'/'.date("Ymd");
        mkdir ($dateDir,true);
    }

    if (!is_dir(public_path().'/'.$type.'/'.date("Ymd").'/thumb')) {
        $thumbDir = public_path().'/'.$type.'/'.date("Ymd").'/thumb';
        mkdir ($thumbDir,true);
    }

    if (!is_dir(public_path().'/'.$type.'/'.date("Ymd").'/small')) {
        $smallDir = public_path().'/'.$type.'/'.date("Ymd").'/small';
        mkdir ($smallDir,true);
    }

    if (!is_dir(public_path().'/'.$type.'/'.date("Ymd").'/thumb'.'/'.$chr)) {
        $chrThumbDir = public_path().'/'.$type.'/'.date("Ymd").'/thumb'.'/'.$chr;
        mkdir ($chrThumbDir,true);
    }

    if (!is_dir(public_path().'/'.$type.'/'.date("Ymd").'/small'.'/'.$chr)) {
        $chrSmallDir = public_path().'/'.$type.'/'.date("Ymd").'/small'.'/'.$chr;
        mkdir ($chrSmallDir,true);
    }
    if (!is_dir(public_path().'/'.$type.'/'.date("Ymd").'/floorPlan')) {
        $floorPlanDir = public_path().'/'.$type.'/'.date("Ymd").'/floorPlan';
        mkdir ($floorPlanDir,true);
    }

    if (!is_dir(public_path().'/'.$type.'/'.date("Ymd").'/floorPlan'.'/'.$chr)) {
        $chrFpDir = public_path().'/'.$type.'/'.date("Ymd").'/floorPlan'.'/'.$chr;
        mkdir ($chrFpDir,true);
    }



    if (!is_dir(public_path().'/'.$type.'/'.date("Ymd").'/locationImg')) {
        $locationImg = public_path().'/'.$type.'/'.date("Ymd").'/locationImg';
        mkdir ($locationImg,true);
    }

    if (!is_dir(public_path().'/'.$type.'/'.date("Ymd").'/locationImg'.'/'.$chr)) {
        $chrLmDir = public_path().'/'.$type.'/'.date("Ymd").'/locationImg'.'/'.$chr;
        mkdir ($chrLmDir,true);
    }


    if (!is_dir(public_path().'/'.$type.'/'.date("Ymd").'/sMImg')) {
        $sMImg = public_path().'/'.$type.'/'.date("Ymd").'/sMImg';
        mkdir ($sMImg,true);
    }

    if (!is_dir(public_path().'/'.$type.'/'.date("Ymd").'/sMImg'.'/'.$chr)) {
        $chrsMImgDir = public_path().'/'.$type.'/'.date("Ymd").'/sMImg'.'/'.$chr;
        mkdir ($chrsMImgDir,true);
    }


    /*=======================================
       =            Product Images            =
     =======================================*/

 $thumbDirPath = date("Ymd").'/thumb/'.$chr;
 $smallDirPath = date("Ymd").'/small/'.$chr;
 if(var_dump($r->hasFile('propertyImages'))){
$propImage = [];
for ($v=0; $v < count($r->propertyImages); $v++) {
    $thums      = $r->propertyImages;

    $filnameSmall = 'small-'.uniqid($chr).'-'.$v.'.'.$thums[$v]->getclientOriginalExtension();
    $img = Image::make($thums[$v]->getRealPath());
    $img->resize(300,null,function ($constraint) {
        $constraint->aspectRatio();
    });
    $smallPath = public_path().'/test_img/'.$filnameSmall;
    if (file_exists($smallPath)) {
         unlink($smallPath);
 }
 $img->save($smallPath);

 $optimizerChain = OptimizerChainFactory::create();
 $optimizerChain->optimize($smallPath);

 $filnameLarge = 'thumb-'.uniqid($chr).'-'.$v.'.'.$thums[$v]->getclientOriginalExtension();
 $img = Image::make($thums[$v]->getRealPath());
 $img->resize(1080,420,function ($constraint) {
    $constraint->aspectRatio();
});
 $thumbPath = public_path().'/test_img/'.$filnameSmall;
 if (file_exists($thumbPath)) {
    unlink($thumbPath);
}
$img->save($thumbPath);
$optimizerChain->optimize($thumbPath);

$propImage[$v]['small'] = $filnameSmall;
$propImage[$v]['smallDirPath'] = $smallDirPath;
$propImage[$v]['large'] = $filnameLarge;
$propImage[$v]['thumbDirPath'] = $thumbDirPath;
}
 }
 else{
$propImage = NULL;
 }
$floorPlanDirPath = date("Ymd").'/floorPlan/'.$chr;
if($r->file('floorPlan')){
$floor = [];

for ($v=0; $v < count($r->floorPlan); $v++) {
    $thums               = $r->floorPlan;
    $floorPlan = 'floor_plan-'.uniqid($chr).'-'.$v.'.'.$thums[$v]->getclientOriginalExtension();
    $img = Image::make($thums[$v]->getRealPath());
    $img->resize(1080,function ($constraint) {
        $constraint->aspectRatio();
    });
    $thumbPath = public_path().'/'.$type.'/'.date("Ymd").'/floorPlan'.'/'.$chr.'/'.$floorPlan;

    if (file_exists($thumbPath)) {
        $this->removeImage($thumbPath);
    }
    $img->save($thumbPath);
    $optimizerChain->optimize($thumbPath);

    $floor[$v]['floor_plan'] = $floorPlan;
    $floor[$v]['floorPlanPath'] = $floorPlanDirPath;
    }
   }else{
  $floor = NULL;
   }
  $sMImgDirPath = date("Ymd").'/sMImg/'.$chr;
  if($r->hasFile('location_images')){
  $sm = [];
  for ($v=0; $v < count($r->siteMapImg); $v++) {
    $thums               = $r->siteMapImg;
    $siteMapImage = 'site_map_image-'.uniqid($chr).'-'.$v.'.'.$thums[$v]->getclientOriginalExtension();
    $img = Image::make($thums[$v]->getRealPath());
    $img->resize(1080,function ($constraint) {
        $constraint->aspectRatio();
    });
    $thumbPath = public_path().'/'.$type.'/'.date("Ymd").'/sMImg'.'/'.$chr.'/'.$siteMapImage;

    if (file_exists($thumbPath)) {
        $this->removeImage($thumbPath);
    }
    $img->save($thumbPath);
    $optimizerChain->optimize($thumbPath);

    $sm[$v]['siteMap'] = $siteMapImage;
    $sm[$v]['siteMapPath'] = $sMImgDirPath;
   }
 }else{
  $sm = NULL;
  }
 if($propImage && count($propImage)!=NULL){
 for ($v=0; $v< count($propImage); $v++) {
 $prop->proImage()->create()([
    'small_thumb'         => $propImage[$v]['small'],'small_thumb_url'     => $propImage[$v]['smallDirPath'],'large_thumb'         => $propImage[$v]['large'],'large_thumb_url'     => $propImage[$v]['thumbDirPath'],]);
}}
for ($v=0; $v < empty($floor); $v++) {
 $prop->floorPlan()->create([
    'floor_plan'         => $floor[$v]['floor_plan'],'floorPlanPath'     => $floor[$v]['floorPlanPath']
]);
}

       /*=============================================
        =            Location Image            =
        =============================================*/

  $locationImgDirPath = date("Ymd").'/locationImg/'.$chr;
  if($r->file('location_images')){
    $locn = [];
    $thums               = $r->location_images;
    $locationImg = 'location_image'.uniqid($chr).'-'.$v.'.'.$thums[$v]- >getclientOriginalExtension();
    $img = Image::make($thums[$v]->getRealPath());
    $img->resize(1080,function ($constraint) {
        $constraint->aspectRatio();
    });
    $thumbPath = public_path().'/'.$type.'/'.date("Ymd").'/locationImg'.'/'.$chr.'/'.$locationImg;

    if (file_exists($thumbPath)) {
        $this->removeImage($thumbPath);
    }
 if(!empty($thumbPath)) {
   if (file_exists(public_path('property_img/'.$thumbPath))) { // unlink or remove previous image from folder
   unlink(public_path('property_img/'.$thumbPath));
   }
    }
    $img->save($thumbPath);
    $optimizerChain->optimize($thumbPath);

    $locn[$v]['location_image'] = $locationImg;
    $locn[$v]['locationImgPath'] = $locationImgDirPath;
  }else{
  $locn = NULL;
  }
  if ($locn && count($locn) == 1) {
  for ($v=0; $v < count($locn); $v++) {
  $prop->mapImage()->create([
    'location_image'         => $locn[$v]['location_image'],'locationImgPath'        => $locn[$v]['locationImgPath']
  ]);
  }
 }
 /*=====  End of Location Image  ======*/
if($sm && count($sm) == 1){
for ($v=0; $v < count($sm); $v++) {
$prop->siteMapImage()->create([
    'siteMap'             => $sm[$v]['siteMap'],'siteMapPath'     => $sm[$v]['siteMapPath']
  ]);
   }
    }
  $prop->save();
  return redirect()->back();
  }
  }
sytsoft 回答:如何解决Laravel中的多个图像编辑/更新问题?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3132967.html

大家都在问