Home > database >  update image on Laravel 9
update image on Laravel 9

Time:09-26

I would like to update image on Laravel.

Now:update method works though image hasn't been changed.

codes are below

Controller

$shop = Shop::find($id);
        $shop->name = $request->input('name');
        $shop->email = $request->input('email');
        $shop->address = $request->input('address');
        $shop->number = $request->input('number');
        $shop->description = $request->input('description');
        
        
        if($request->hasFile('image'))
        {
            $destination = 'storage/shops/' . $shop->image;
            if(File::exists($destination))
            {
                File::delete($destination);
            }
            $file = $request->file('image');
            $extension = $file->getClientOriginalExtension();
            $filename = time(). '.' . $extension;
            $file->move('storage/shops/', $filename);
            $shop->image = $filename;
        }
        $shop->update();
        return redirect()->route('shops');

Blade file are below

 @foreach($shops as $shop)
      <img src="{{ asset('storage/shops/' . $shop->image) }}"width="880" height="650"  alt="No image" width="880" height="650">
      <div >
      <h1 >
       <a href="{{ route('shops_show', $shop->id) }}"><h5 >{{ $shop->name }}</h5></a>
      </h1>
      <div >
        <p >{{ $shop->description }}</p>  
      </div>
    </div>
    @endforeach

I tried rewriting of slash or taking from DB of $destination but it doesn't work.

e.g. $destination = '/storage/shops/' . $shop->image;

CodePudding user response:

The asset is only used for files in the public folder, so if you save it in storage you won't be able to get it out. I will give you an example of storing files in the public folder.

In config/filesystems.php

    'disks' => [
        'public' => [
            'driver' => 'local',
            'root' => public_path() . '/uploads',
            'url' => env('APP_URL'),
            'visibility' => 'public',
        ],
    ],

In upload service

public function uploadFile(){
        try {
                $randomKey = bin2hex(random_bytes(3));
                $file = request()->file('file');
                //1048576,2 = 1mb
                $maxUpload = 30 * 1048576;
                if ($file->getSize() > $maxUpload) return response()->json('Max Upload', 422);
                $explodeOriginalNam = explode(".", $file->getClientOriginalName());
                $name = $file->getClientOriginalName();
                $path = Str::slug($explodeOriginalNam[0]).$randomKey.'.'.$file->getClientOriginalExtension();
                if (!Storage::disk('new_local')->put($path, file_get_contents($file))){
                    return response()->json(false, 500);
                }
                return response()->json(['name' => $name, 'path' => "/uploads/$path", 'preview' => asset("/uploads/$path")]);
        } catch (\Exception $e){
            return response()->json(false, 500);
        }
    }

Then you can get the image with assets normally. Hope it help u and happy coding !

CodePudding user response:

use this Package is dynamic relationship with any model

GitHub : Media Package

reading more in this article Media Package

CodePudding user response:

Have you linked your storage? Try php artisan storage:link and then try to access from public folder.

CodePudding user response:

  1. I didn't put enctype on editing page inside of form tag.

  2. the way to retrieve value of image was a bit wrong in controller

  • Related