See images stored in a view of Laravel

0

You see, I have a table of plants which has the following properties:

Schema::create('plantas', function (Blueprint $table){
            $table->increments('id');
            $table->string('nombre'); // Nombre de la planta.
            $table->string('tamaño'); // Clasifica segun si es arbol, arbusto o hierba. 
            $table->string('flor'); // Si tiene o no flor.
            $table->string('hoja'); // Si es de hoja caduca o perenne.
            $table->text('descripcion'); // Caracteristicas del vegetal.
            $table->string('foto')->nullable(); // Esta variable sera utilizada para almacenar fotos. Es opcional.
            $table->timestamps();
        });

I have to create a view in which the characteristics of each plant are shown, including a photo of the plant (if it has one). Here I show the view:

@extends('layouts.app')
@section('content')
<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <div align="center" class="panel panel-default">
            <div class="panel-heading">
                <h1 class="text-center text-mute"> {{ __("Plantas") }} </h1>
            </div>
            <div class="panel-body">
                @Logged()
                    <a href="formulario/plantas">Crear una nueva planta</a>
                    @include('partials.errors')
                @else
                    <p style="color:#0000FF">Para poner insertar nuevas plantas tienes que iniciar sesión</p>
                @endLogged
            </div>
        </div>

        @forelse($planta as $plantas)
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3><a href="comentarios/{{ $plantas->id }}">{{ $plantas->nombre }}</a></h3>
            </div>
            <div class="panel-body">
                <h4>{{ $plantas->descripcion }}</h4>
                @if($plantas->attachment)
                    <img src="{{ $plantas->pathAttachment() }}" class="img-responsive img-rounded" />
                @endif
            </div>
            <div class="panel-body">
                <b>Tamaño:</b> {{ $plantas->tamaño }}<br>
                <b>Flor:</b> {{ $plantas->flor }}<br>
                <b>Hoja:</b> {{ $plantas->hoja }}<br>
                <span class="pull-right"> {{ __("Comentarios") }}: {{ $plantas->comentarios->count() }} </span>
            </div>
        </div>
        @empty
        <div class="alert alert-danger">
            {{ __("No hay ninguna planta en este momento") }}
        </div>
        @endforelse
        @if($planta->count())
        {{$planta->links()}}
        @endif
    </div>
</div>
</div>
</div>
@endsection

web.php:

Route::get('/images/{path}/{attachment}', function ($path, $attachment){
    $storagePath = Storage::disk($path)->getDriver()->getAdapter()->getPathPrefix();
    $imageFilePath = $storagePath . $attachment;
    if(File::exists($imageFilePath)) {
        return Image::make($imageFilePath)->response();
    }
});

plantas.php:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class plantas extends Model{
    protected $table = 'plantas';

    protected $fillable = [
        'nombre', 'tamaño', 'flor', 'hoja', 'descripcion', 'foto',
    ];

    public function comentarios(){
        return $this->hasMany(Comentario::class,'planta');
    }

    public function pathAttachment(){
        return "/images/planta/" . $this->attachment;
    }
}

However, I still can not see the images.

What will I be leaving?

Edit: Here you have filesystems.php.

<?php

return [

    'default' => env('FILESYSTEM_DRIVER', 'local'),

    'cloud' => env('FILESYSTEM_CLOUD', 's3'),

    'disks' => [

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

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

        'planta' => [
            'driver' => 'local',
            'root' => storage_path('app/planta'),
            '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'),
        ],

    ],

];

More things: Once I upload the image with my form, when I look at the route in phpMyAdmin I see that the following route appears: * C: \ xampp \ tmp * However, I go to the indicated route and the images do not appear. Will it have to do with my problems?

Also another detail. I've tried putting the following in the view:

{{$ plants-> pathAttachment ()}}

And look at the result:

CASI manages to put the path where I currently keep the image, but at the end there it stayed.

    
asked by Miguel Alparez 14.02.2018 в 15:14
source

1 answer

0

You must have the images inside the storage/public folder directly or inside any folder you want within storage/public . In your case, if I have not misunderstood storage/public/images/planta and to show them:

<img src="{{ $plantas->pathAttachment() }}" class="img-responsive img-rounded" />

The framework will search for public directly. If you have done the symlink you will find it at storage/public so it is in this route from which you must work.

A similar case: as open a preview of an image saved in laravel?

    
answered by 14.02.2018 в 15:49