0
votes

I can't display my images in a tag img in my view. I can save my uploaded image in storage->app->public->books and save the image name in my database as well. But after I run php artisan storage:link to access my file in public->storage, but whatever if I user asset() or url() method, I can't display my images in img tag, just alt="" atribute, Someone knows what I'm doing wrong?

This is my Controller store().

public function store(Request $request)
{
    // Handle File Upload
    if($request->hasFile('book_image')){
        // Get filename with the extension
        $filenameWithExt = $request->file('book_image')->getClientOriginalName();
        // Get just filename
        $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
        // Get just ext
        $extension = $request->file('book_image')->getClientOriginalExtension();
        // Filename to store
        $fileNameToStore= $filename.'_'.time().'.'.$extension;
        // Upload Image
        $path = $request->file('book_image')->storeAs('books', $fileNameToStore);
    } else {
        $fileNameToStore = 'noimage.png';
    }
    //save in database
    $itens = \App\Book::create([
        'book_name' => mb_strtolower($request->book_name),
        'author_name' => mb_strtolower($request->author_name),
        'publishing_name' => mb_strtolower($request->publishing_name),
        'description' => $request->description,
        'release_date' => $request->release_date,
        'category' => mb_strtolower($request->category),
        'book_image' => $fileNameToStore
    ]);

    flash('Livro criado com sucesso !')->success();

    return redirect()->route('admin.books.index');
}

And this is my html where I'm trying to display.

   @foreach($books as $book)
    <tr>
        <td><input type="checkbox" class="checkthis" /></td>
        <td>
            <img style="width: 80px;height: 80px" src="{{ url('books/{$book->book_image}') }}" alt="{{$book->book_image}}">
        </td>
        <td>{{$book->book_name}}</td>
        <td>{{$book->author_name}}</td>
        <td>{{$book->publishing_name}}</td>
        <td>{{$book->release_date}}</td>
        <td>
   </tr>

And my config->filesystems.php

    'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
        'endpoint' => env('AWS_ENDPOINT'),
    ],

],
1

1 Answers

0
votes

You need to pass src as below.

 src="{{ url('books/'.$book->book_image) }}"

as storage folder

src="{{ asset('storage/books/'.$book->book_image) }}"