Laravel 5.6: When I upload an image, it saves a path but not an image

0

When I upload an image, it perfectly keeps the image's path in the database, whose field is "image", but it does not save the image as such in the public folder as specified in the code.

In the create view of my project I use Form, so I read that I had to modify it in the following way to upload files:

{!! Form::open(['route' => 'products.store', 'files' => True]) !!}

In the controller I have the following for the "store" and "update" methods:

//IMAGE
    if($request->file('image')){

        $path = Storage::disk('public')->put('image', $request->file('image'));

        $product->fill(['image' => asset($path)])->save();
    }

Neither is the type of image validating me, but the other fields do. I leave the validation code:

public function rules()
{
    return [
        'name'  => 'required',
        'short' => 'required',
        'body' => 'required'
    ];

if($this->get('image'))
        $rules = array_merge($rules, ['image' => 'mimes:jpg,jpeg,png']);

    return $rules;

}

Now configure the config fylesystems by placing the root path as follows:

'root' => public_path(),
    
asked by Kinafune 02.07.2018 в 02:47
source

1 answer

0

You have to run the following command in your project

php artisan storage:link

This command creates a folder called storage in the public folder, from there you can access the images since laravel is responsible for moving the image to this folder by itself.

Regarding the validation of the image, add this to the rules

'imagen'          => 'image|max:1999|required'

That validates that it is image, with a maximum size, and required.

To validate the type, you can make a custom rule from laravel link

If you create the rule, the validation would be as follows

'imagen'          => ['image','max:1999','required', new miCustomRule]

And remember to use the file

use App\Rules\miCustomRule;

I hope it serves you.

    
answered by 02.07.2018 / 06:42
source