Kavyajain's avatar

laravel backpack File Upload

In my controller I have,

$this->crud->addField([ // image
            'label' => "Thumbnail Image",
            'name' => "thumbnail_image",
            'type' => 'image',
            'upload' => true,
            'disk' => 'uploads'
        ],'both');

In model,

public function setImageAttribute($value)
    {
        $attribute_name = "image";
        $disk = "public";
        $destination_path = "/uploads";       
        $this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);
    }

In filesystem.php 'disks' => [

'disks' => [
.
.
.
'uploads' => [
            'driver' => 'local',
            'root'   => public_path() . '/uploads',
        ],
],

Error I am getting is 
SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'thumbnail_image' at row 1 (SQL: update `room_categories` set `thumbnail_image` = data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEDIAMgAA.................

Please Help

0 likes
8 replies
pardeepkumar's avatar

File upload is an essential aspect of any project. Given this importance, it is surprising that many developers face challenges of adding file upload feature to their projects. In particular, developers are unsure about how to upload and validate files. If you want to upload big files you should use streams. Here’s the code to do it:

I suggest you to follow this article steps:

https://www.cloudways.com/blog/laravel-multiple-files-images-upload/

Kavyajain's avatar

Thank You for that link. But, I want to use it with laravel backpack and not custom blade file

s4muel's avatar

i haven't seen backpack before, but this pops-out for me in here:

public function setImageAttribute($value)  
{  
  $attribute_name = "image";  
  $disk = "public";  
  $destination_path = "/uploads";  
  $this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);  
}

You use setImageAttribute and set the name to image $attribute_name = "image";, shouldn't it be thumbnail_image?

Kavyajain's avatar
public function setthumbnail_imageAttribute($value)
    {
        $attribute_name = "thumbnail_image";
        $disk = "public";
        $destination_path = "uploads";

        $this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);
    }

Ok. so this one is saving but not the filename

It is saving this D:\xampp\tmp\phpBC6B.tmp filename being Admissions.jpg

s4muel's avatar
s4muel
Best Answer
Level 50

just shooting from the hip, have you tried setThumbnailImageAttribute($value) instead of setthumbnail_imageAttribute($value)?

1 like
Kavyajain's avatar

Omg. You are Amazing.. It is now entering the function but saving the filename incorrect.

public function setThumbnailImageAttribute($value) {

        $image=$value;
        $input['thumbnail_image'] = time().'.'.$image->getClientOriginalExtension();
        $img = \Image::make($image->getRealPath());

        $destinationPath = public_path('/uploads/product');
        $img->resize(750, 450, function ($constraint) {
        $constraint->aspectRatio();
        })->save($destinationPath.'/'.$input['thumbnail_image']);

        $destinationPath = public_path('/uploads/product');

        $img->resize(100, 100, function ($constraint) {
        $constraint->aspectRatio();
        })->save($destinationPath.'/'.$input['thumbnail_image']);

        $image->move($destinationPath, $input['thumbnail_image']);
        $this->attributes['thumbnail_image'] = strtolower($input['thumbnail_image']);

    }

Please help

Kavyajain's avatar

Found a solution. Many thanks

public function setThumbnailImageAttribute($value) {

        $image=$value;
        $input['thumbnail_image'] = $image->getClientOriginalName();
        $img = \Image::make($image->getRealPath());

        $destinationPath = public_path('/uploads/thumbnailImages');
        $img->resize(750, 450, function ($constraint) {
        $constraint->aspectRatio();
        })->save($destinationPath.'/'.$input['thumbnail_image']);

        $destinationPath = public_path('/uploads/thumbnailImages');

        $img->resize(100, 100, function ($constraint) {
        $constraint->aspectRatio();
        })->save($destinationPath.'/'.$input['thumbnail_image']);

        $image->move($destinationPath, $input['thumbnail_image']);
        $this->attributes['thumbnail_image'] = $input['thumbnail_image'];

    }
1 like
yograj's avatar

Found Simple Solution:

public function setImageAttribute($value)
{
    if ($value) {
        // destination folder to store image
        $prefix = 'images/';

        // Delete the previous image if exists (for Update Operation)
        if ($this->image) {
            $previousImagePath = public_path($prefix) . $this->image;
            if (file_exists($previousImagePath)) {
                unlink($previousImagePath);
            }
        }

        $fileName = time() . '_' . uniqid() . '.' . $value->getClientOriginalExtension();
        $value->move(public_path($prefix), $fileName);
        $this->attributes['image'] = $fileName;
    }
}

Please or to participate in this conversation.