unlink():无效参数更新laravel中的图像时发生此错误

azpvetkf  于 2023-01-06  发布在  其他
关注(0)|答案(2)|浏览(127)

我正在尝试将图像更新到image/brand/ folder,但发生了意外错误。

public function update(Request $request,$id){

        $validated = $request->validate([
            'brand_name' => 'required|max:4',
            // 'brand_image' => 'required|mimes:jpg,jpeg,png',
        ]);

        $old_image=$request->old_image;
        $brandimage=$request->file('brand_image');

        $image_gen=hexdec(uniqid());
        $image_exten=strtolower($brandimage->getClientOriginalExtension());
        $image_name=$image_gen.'.'.$image_exten;
        $image_location='image/brand/';
        $image_uplioad= $image_location.$image_name;

        $brandimage->move($image_location,$image_name);

        unlink($old_image);

        Brand::find($id)->update([
            'brand_name' =>$request->brand_name,
            'brand_image'=>$image_uplioad,
            'Created_at'=> Carbon::Now()
        ]);

        return  Redirect()->back()->with('success','Brand image updated Successfully');

    }

错误异常
unlink():无效参数,这是我得到的错误,我需要克服这个问题,请帮助

3zwjbxry

3zwjbxry1#

您可以通过在model内部创建updateImage方法来更新model中的映像,从而优化它,如下所示

品牌型号

.....
    public function updateImage($image)
    {
        $oldImage = $this->brand_image?? '';
      
        $this->brand_image = $image->store('brand', 'public');

        $this->save();

        Storage::disk('public')->delete($oldImage);
    }
.....

之后控制器将是这样的

$validated = $request->validate([
        'brand_name' => 'required|max:4',
        // 'brand_image' => 'required|mimes:jpg,jpeg,png',
    ]);

    $brand = Brand::find($id)->update([
        'brand_name' =>$request->brand_name,
        'Created_at'=> Carbon::Now()
    ]);

    if($request->hasFile('brand_image'))
        $brand->updateImage($request->file('brand_image'));

    return  Redirect()->back()->with('success','Brand image updated Successfully');
fivyi3re

fivyi3re2#

请从刀片中传递$old_image=$request->old_image;值。

相关问题