Laravel Backpack not uploading image












0















I am attempting to upload a file (country flag) to a simple table countries which should be saved in a "flags" folder of public.



In my add field declaration I have



$this->crud->addField([ // image
'label' => "flag",
'name' => "flag",
'type' => 'image',
'upload' => true,
'disk' => 'flags', // in case you need to show images from a different disk
'prefix' => 'flags/'


and in the filesystems file I have:



'flags' => [
'driver' => 'local',
'root' => public_path('flags'),
'url' => '/flags',
'visibility' => 'public',
],


When I upload it tells me that the field is too short (it is varchar 255) as it seems to want to store the file as data image.










share|improve this question























  • I have just placed dd($request) and the flag image is being converted: "flag" => "data:image/gif;base64,R0lGODlhBAIEAfcAMf/////39//v7//n5//e5//e3vf3//f39/fv9/fv7/fn7/fW3vfW1ve9vfe1ve/v9+/v7+/v5+/n5+/e5+/W1u+tte+cpe+Upe+UnO+MnO+ElO97Iefn9+fn7+fn5+fn3ufe3ufOteetteetred7hOd7Ied7GOdzhOdzGOdjc+dja+daa97e797e597e3t7e1t7W1t57hN5zId5zGN5re95jc95S....

    – Jim
    Nov 25 '18 at 17:34


















0















I am attempting to upload a file (country flag) to a simple table countries which should be saved in a "flags" folder of public.



In my add field declaration I have



$this->crud->addField([ // image
'label' => "flag",
'name' => "flag",
'type' => 'image',
'upload' => true,
'disk' => 'flags', // in case you need to show images from a different disk
'prefix' => 'flags/'


and in the filesystems file I have:



'flags' => [
'driver' => 'local',
'root' => public_path('flags'),
'url' => '/flags',
'visibility' => 'public',
],


When I upload it tells me that the field is too short (it is varchar 255) as it seems to want to store the file as data image.










share|improve this question























  • I have just placed dd($request) and the flag image is being converted: "flag" => "data:image/gif;base64,R0lGODlhBAIEAfcAMf/////39//v7//n5//e5//e3vf3//f39/fv9/fv7/fn7/fW3vfW1ve9vfe1ve/v9+/v7+/v5+/n5+/e5+/W1u+tte+cpe+Upe+UnO+MnO+ElO97Iefn9+fn7+fn5+fn3ufe3ufOteetteetred7hOd7Ied7GOdzhOdzGOdjc+dja+daa97e797e597e3t7e1t7W1t57hN5zId5zGN5re95jc95S....

    – Jim
    Nov 25 '18 at 17:34
















0












0








0








I am attempting to upload a file (country flag) to a simple table countries which should be saved in a "flags" folder of public.



In my add field declaration I have



$this->crud->addField([ // image
'label' => "flag",
'name' => "flag",
'type' => 'image',
'upload' => true,
'disk' => 'flags', // in case you need to show images from a different disk
'prefix' => 'flags/'


and in the filesystems file I have:



'flags' => [
'driver' => 'local',
'root' => public_path('flags'),
'url' => '/flags',
'visibility' => 'public',
],


When I upload it tells me that the field is too short (it is varchar 255) as it seems to want to store the file as data image.










share|improve this question














I am attempting to upload a file (country flag) to a simple table countries which should be saved in a "flags" folder of public.



In my add field declaration I have



$this->crud->addField([ // image
'label' => "flag",
'name' => "flag",
'type' => 'image',
'upload' => true,
'disk' => 'flags', // in case you need to show images from a different disk
'prefix' => 'flags/'


and in the filesystems file I have:



'flags' => [
'driver' => 'local',
'root' => public_path('flags'),
'url' => '/flags',
'visibility' => 'public',
],


When I upload it tells me that the field is too short (it is varchar 255) as it seems to want to store the file as data image.







laravel-5 backpack-for-laravel






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 8:46









JimJim

81214




81214













  • I have just placed dd($request) and the flag image is being converted: "flag" => "data:image/gif;base64,R0lGODlhBAIEAfcAMf/////39//v7//n5//e5//e3vf3//f39/fv9/fv7/fn7/fW3vfW1ve9vfe1ve/v9+/v7+/v5+/n5+/e5+/W1u+tte+cpe+Upe+UnO+MnO+ElO97Iefn9+fn7+fn5+fn3ufe3ufOteetteetred7hOd7Ied7GOdzhOdzGOdjc+dja+daa97e797e597e3t7e1t7W1t57hN5zId5zGN5re95jc95S....

    – Jim
    Nov 25 '18 at 17:34





















  • I have just placed dd($request) and the flag image is being converted: "flag" => "data:image/gif;base64,R0lGODlhBAIEAfcAMf/////39//v7//n5//e5//e3vf3//f39/fv9/fv7/fn7/fW3vfW1ve9vfe1ve/v9+/v7+/v5+/n5+/e5+/W1u+tte+cpe+Upe+UnO+MnO+ElO97Iefn9+fn7+fn5+fn3ufe3ufOteetteetred7hOd7Ied7GOdzhOdzGOdjc+dja+daa97e797e597e3t7e1t7W1t57hN5zId5zGN5re95jc95S....

    – Jim
    Nov 25 '18 at 17:34



















I have just placed dd($request) and the flag image is being converted: "flag" => "data:image/gif;base64,R0lGODlhBAIEAfcAMf/////39//v7//n5//e5//e3vf3//f39/fv9/fv7/fn7/fW3vfW1ve9vfe1ve/v9+/v7+/v5+/n5+/e5+/W1u+tte+cpe+Upe+UnO+MnO+ElO97Iefn9+fn7+fn5+fn3ufe3ufOteetteetred7hOd7Ied7GOdzhOdzGOdjc+dja+daa97e797e597e3t7e1t7W1t57hN5zId5zGN5re95jc95S....

– Jim
Nov 25 '18 at 17:34







I have just placed dd($request) and the flag image is being converted: "flag" => "data:image/gif;base64,R0lGODlhBAIEAfcAMf/////39//v7//n5//e5//e3vf3//f39/fv9/fv7/fn7/fW3vfW1ve9vfe1ve/v9+/v7+/v5+/n5+/e5+/W1u+tte+cpe+Upe+UnO+MnO+ElO97Iefn9+fn7+fn5+fn3ufe3ufOteetteetred7hOd7Ied7GOdzhOdzGOdjc+dja+daa97e797e597e3t7e1t7W1t57hN5zId5zGN5re95jc95S....

– Jim
Nov 25 '18 at 17:34














1 Answer
1






active

oldest

votes


















0














You should take another look at all the instructions for the image field type, in the documentation. Backpack does not take care of the uploading for you - you need an accessor on your Model, so you can choose where it's uploaded and how. If you don't do that, Backpack will try to store it as Base64 in your database - which isn't a good idea in most cases.



Example accessor for flag:



public function setFlagAttribute($value)
{
$attribute_name = "flag";
$disk = "public_folder";
$destination_path = "uploads/folder_1/subfolder_3";

// if the image was erased
if ($value==null) {
// delete the image from disk
Storage::disk($disk)->delete($this->{$attribute_name});

// set null in the database column
$this->attributes[$attribute_name] = null;
}

// if a base64 was sent, store it in the db
if (starts_with($value, 'data:image'))
{
// 0. Make the image
$image = Image::make($value)->encode('jpg', 90);
// 1. Generate a filename.
$filename = md5($value.time()).'.jpg';
// 2. Store the image on disk.
Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
// 3. Save the path to the database
$this->attributes[$attribute_name] = $destination_path.'/'.$filename;
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465942%2flaravel-backpack-not-uploading-image%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You should take another look at all the instructions for the image field type, in the documentation. Backpack does not take care of the uploading for you - you need an accessor on your Model, so you can choose where it's uploaded and how. If you don't do that, Backpack will try to store it as Base64 in your database - which isn't a good idea in most cases.



    Example accessor for flag:



    public function setFlagAttribute($value)
    {
    $attribute_name = "flag";
    $disk = "public_folder";
    $destination_path = "uploads/folder_1/subfolder_3";

    // if the image was erased
    if ($value==null) {
    // delete the image from disk
    Storage::disk($disk)->delete($this->{$attribute_name});

    // set null in the database column
    $this->attributes[$attribute_name] = null;
    }

    // if a base64 was sent, store it in the db
    if (starts_with($value, 'data:image'))
    {
    // 0. Make the image
    $image = Image::make($value)->encode('jpg', 90);
    // 1. Generate a filename.
    $filename = md5($value.time()).'.jpg';
    // 2. Store the image on disk.
    Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
    // 3. Save the path to the database
    $this->attributes[$attribute_name] = $destination_path.'/'.$filename;
    }
    }





    share|improve this answer




























      0














      You should take another look at all the instructions for the image field type, in the documentation. Backpack does not take care of the uploading for you - you need an accessor on your Model, so you can choose where it's uploaded and how. If you don't do that, Backpack will try to store it as Base64 in your database - which isn't a good idea in most cases.



      Example accessor for flag:



      public function setFlagAttribute($value)
      {
      $attribute_name = "flag";
      $disk = "public_folder";
      $destination_path = "uploads/folder_1/subfolder_3";

      // if the image was erased
      if ($value==null) {
      // delete the image from disk
      Storage::disk($disk)->delete($this->{$attribute_name});

      // set null in the database column
      $this->attributes[$attribute_name] = null;
      }

      // if a base64 was sent, store it in the db
      if (starts_with($value, 'data:image'))
      {
      // 0. Make the image
      $image = Image::make($value)->encode('jpg', 90);
      // 1. Generate a filename.
      $filename = md5($value.time()).'.jpg';
      // 2. Store the image on disk.
      Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
      // 3. Save the path to the database
      $this->attributes[$attribute_name] = $destination_path.'/'.$filename;
      }
      }





      share|improve this answer


























        0












        0








        0







        You should take another look at all the instructions for the image field type, in the documentation. Backpack does not take care of the uploading for you - you need an accessor on your Model, so you can choose where it's uploaded and how. If you don't do that, Backpack will try to store it as Base64 in your database - which isn't a good idea in most cases.



        Example accessor for flag:



        public function setFlagAttribute($value)
        {
        $attribute_name = "flag";
        $disk = "public_folder";
        $destination_path = "uploads/folder_1/subfolder_3";

        // if the image was erased
        if ($value==null) {
        // delete the image from disk
        Storage::disk($disk)->delete($this->{$attribute_name});

        // set null in the database column
        $this->attributes[$attribute_name] = null;
        }

        // if a base64 was sent, store it in the db
        if (starts_with($value, 'data:image'))
        {
        // 0. Make the image
        $image = Image::make($value)->encode('jpg', 90);
        // 1. Generate a filename.
        $filename = md5($value.time()).'.jpg';
        // 2. Store the image on disk.
        Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
        // 3. Save the path to the database
        $this->attributes[$attribute_name] = $destination_path.'/'.$filename;
        }
        }





        share|improve this answer













        You should take another look at all the instructions for the image field type, in the documentation. Backpack does not take care of the uploading for you - you need an accessor on your Model, so you can choose where it's uploaded and how. If you don't do that, Backpack will try to store it as Base64 in your database - which isn't a good idea in most cases.



        Example accessor for flag:



        public function setFlagAttribute($value)
        {
        $attribute_name = "flag";
        $disk = "public_folder";
        $destination_path = "uploads/folder_1/subfolder_3";

        // if the image was erased
        if ($value==null) {
        // delete the image from disk
        Storage::disk($disk)->delete($this->{$attribute_name});

        // set null in the database column
        $this->attributes[$attribute_name] = null;
        }

        // if a base64 was sent, store it in the db
        if (starts_with($value, 'data:image'))
        {
        // 0. Make the image
        $image = Image::make($value)->encode('jpg', 90);
        // 1. Generate a filename.
        $filename = md5($value.time()).'.jpg';
        // 2. Store the image on disk.
        Storage::disk($disk)->put($destination_path.'/'.$filename, $image->stream());
        // 3. Save the path to the database
        $this->attributes[$attribute_name] = $destination_path.'/'.$filename;
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 1 '18 at 8:56









        tabacitutabacitu

        2,13511525




        2,13511525
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465942%2flaravel-backpack-not-uploading-image%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga