Problem with upload using Laravel's store method

0

I have a question with uploads of files in Laravel. I created the symbolic link with php artisan storage:link and to upload files I am using the code below according to the Laravel doc link

public function updatePhoto(Request $request)
{
    $messages = [
        'photo.image' => 'Extensões aceitas: jpg, png, ou gif',
        'photo.max' => 'Tamanho máximo do arquivo: 2MB',
        'photo.uploaded' => 'Tamanho máximo do arquivo: 2MB',
    ];
    $validatePhoto = $request->validate([
        'photo' => 'image|max:2000',
    ], $messages);

    $user = Auth::user();
    $customer = $user->userable;
    if (file_exists($customer->photo)) {
        unlink($customer->photo);
    }
    $path = $request->file('photo')->store('public');
    $path = str_replace('public', 'storage', $path);
    $customer->photo = $path;
    $customer->save();
    Session::flash('alert-success', 'Foto alterada com sucesso!'); 
    return redirect()->back();
}

This stores the image in storage / app / public and saves the path to the database: public / imagem.jpeg which is what I need.

To display the image I'm using:

{{ asset(Auth::user()->userable->photo) }}

The problem is that without using str_replace () to rename public / imagem.jpg to storage / imagem.jpg the image does not appear. How do I display the image using this Laravel store ('public') method without needing to use str_replace ()?

    
asked by anonymous 03.11.2017 / 13:14

1 answer

0

Assuming you have saved the file1.jpg file in the public folder of the storage.You can use Storage::url() to get the url to the file:

$url = Storage::url('file1.jpg');

This way you would not have to concatenate the string 'public' in the path of the file to be saved in the database.

    
09.11.2017 / 19:41