Search code examples
phplaravellaravel-5laravel-5.5laravel-5.6

Laravel Image Update


I'm doing a Admin Panel in Laravel 5.6 and I have a problem where productController --resource update.

My store as this:

if($request->hasfile('filename'))
        {
        $file = $request->file('filename');        
        $name=$file->getClientOriginalName();        
        $image_resize = Image::make($file->getRealPath());              
        $image_resize->resize(590, 590);
        $image_resize->save(public_path('/images/urunler/' .$name));
        } 
        $urun = new Urun();
        $urun->k_id=$request->get('k_id'); 
        $urun->ad=$request->get('ad'); 
        $urun->form=$request->get('form');
        $urun->icerik=$request->get('icerik');                
        $urun->ticari_sekli=$request->get('ticari_sekli');
        $urun->filename=$name;
        $urun->save(); 

        return redirect('/urunler')->with('success','Ürün Başarı İle Eklendi.');

and My Update as this:

$urun= Urun::findOrFail($id);  
$urun->update($request->all());

if ($request->hasFile('filename'))
{
    $file = $request->file('filename');        
    $name=$file->getClientOriginalName();        
    $img = Image::make($file->getRealPath());              
    $img->resize(590, 590);                                                                         
    $img = Image::make($file->getRealPath())->resize(590, 590)->insert(public_path('/images/urunler/' .$name));                                                 
    $img->save(public_path('/images/urunler/' .$name));   
    $urun->image = $name;                        
}   
$urun->save();
return redirect('/urunler')->with('success','Ürün Başarı İle Güncellendi.');

Store part is working but Update part save a filename database but image doesn't move in images/urunler.

My php version is 7.2.4 and I'm using Laravel 5.6.


Solution

  • Delete these codes.

    $img = Image::make($file->getRealPath())->resize(590, 590)->insert(public_path('/images/urunler/' .$name));                                                 
    $img->save(public_path('/images/urunler/' .$name));  
    

    After that. Add these codes.

    $location = public_path('/images/urunler/'.$name);
    Image::make($file->getRealPath())->resize(590, 590)->save($location); 
    

    Give it a try.

    Solution: Change related codes like that.

    $location = public_path('/images/urunler/'.$name);
    Image::make($file->getRealPath())->resize(590, 590)->save($location);
    $urun->filename= $name;   
    

    Then add

     $this->validate(request(),[ 
     'k_id' => 'required',
     'ad' => 'required',
     'form' => 'required',
     'icerik' => 'required',
     'ticari_sekli' => 'required',
     'filename'=>'required' ]);
    

    PS: filename must be just required. Not required|image. It isn't an image. Its just name. After that request filename. Like that: $urun->filename= $name;

    don't $urun->image= $name;. Because there is no $urun->image.