Home > Enterprise >  Update page - Change image name follow video id Laravel
Update page - Change image name follow video id Laravel

Time:11-25

I want change image name follow the video id in Laravel project. How to solve this?

Here is codes of controller

$old_video = Video::find($id);

          //video thumbnail uploaded
          if ($request->hasFile('image_path') != '') {
            $video_themnull = $request->file('image_path');
            $video_themnull_name =  uniqid() . Str::random('10') . '.' . $video_themnull->getClientOriginalExtension();
            $video_themnull_resize = Image::make($video_themnull->getRealPath());
            $video_themnull_resize->resize(400, 200);

          if ($video_themnull->isValid()) {

              if (isset($old_video->image_path)) {
                  $files_old = $old_video->image_path;
                  if ( file_exists($files_old)) {
                      unlink($files_old);
                  }
              }

              $video_themnull_resize->save(public_path('video/themnull/' . $video_themnull_name));
              $image_path = 'public/video/themnull/' . $video_themnull_name;

          }
        }

CodePudding user response:

in your code

$video_themnull_name = uniqid() . Str::random('10') . '.' . $video_themnull->getClientOriginalExtension();

you created the random file name if you want to add video id with your file name then you should add $old_video->id to your file name creation. so please rewrite above code like below

$video_themnull_name = $old_video->id. Str::random('10') . '.' . $video_themnull->getClientOriginalExtension(); 

and add below code inside if ($video_themnull->isValid()) condition

old_video->image_path = $image_path; 
$old_video->save(); 
  • Related