Laravel 5.3 - Issue displaying Images from public folder using @extends & @sections

前端 未结 3 884
再見小時候
再見小時候 2020-12-21 20:14

2nd Update:

For some reason the display: table; in my .header_table within home.blade.php was preventing the image from rendering.

相关标签:
3条回答
  • 2020-12-21 20:41

    If you are using LaravelCollective's Html Facade, you have used an extra directory in your image path.

    {{ Html::image('public/images/max.jpg') }}
    

    Which should be:

    {{ Html::image('images/max.jpg') }}
    

    As Html Facade suppose you are inside the public directory and searches the image from the specified location.

    Edit:

    You are using: @extends('main')

    It supposes you have a main.blade.php in your resources/views/main.

    But you have your main.blade.php inside views/vendor/main.blade.php

    You could try:

    @extends(vendor.main)
    

    Also you are doing something wrong. You are adding all the scripts and css from the child view to parent view.

    When you are using in this format it renders to this form of your html.

    Updated Example:

    My views path:

    |-resources
        |- views
             |- pages
                 |- home.blade.php
             |- welcome.blade.php
    

    welcome.blade.php

    <!DOCTYPE html>
    <html lang="en">
        <head>
        </head>
        <body>
            <div class="flex-center position-ref full-height">
                <div class="col-md-12">
                <p>Image from Parent</p>
                {{ Html::image('images/capture.png') }}
                </div>
                <div class="content">
                    @yield('content')
                </div>
            </div>
        </body>
    </html>
    

    home.blade.php:

    @extends('welcome')
    
    @section('content')
        <div class="col-md-12">
        <p>This is a child view.</p>
        {{ Html::image('images/capture.png') }}
        </div>
    @stop
    

    routes/web.php:

    <?php
    
    Route::get('/', function () {
        return view('pages.home');
    });
    

    I prefer using @stop instead of @endsection. You could try both to test it.

    Hope it helps you. Not confirmed why the image is not rendered.

    If it doesn't render could you add the content of <image src> from your browser?

    Final Output:

    0 讨论(0)
  • 2020-12-21 20:44

    To generate path till public directory you have to use asset() helper methord, and to display image you have to echo that path inside img src attribute.

    This should work for you.

    <img src="{{asset('images/max.jpg')}}">
    

    Assuming you have max.jpg in your_project/public/images/max.jpg.
    Hope this helps!

    0 讨论(0)
  • 2020-12-21 20:47

    Most convenient way to way to store and access images is by using Laravel filesystems.

    First you got to set up your file driver driver config/filesystems.php

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

    This will enable you to store your images in storage/app/public

    Lets say you are storing images in storage/app/public/your_storage_directory directory. So do as follows.

    $image = request()->file('banner');
    $path = $image->store('your_storage_directory', 'public');
    // persist $path in your table
    

    $path will contain someting simillar to your_storage_directory/3ca37bc0cdf2f71eeadf60057c71154b.jpeg

    Then do

    php artisan storage:link
    

    To create a symbolic link at public directory pointing to storage/app/public

    Then you only got to do is access the image from your blade file by using following syntax

    <img src="{{asset($path)}}">
    
    0 讨论(0)
提交回复
热议问题