You see, I'm creating a form to store the data of a plant, in which you have to capture an image. The code is this:
@extends('layouts.app')
@section('content')
@Logged()
@include('partials.errors')
<div align="center" class="panel panel-default">
<h1 class="text-center text-mute"> {{ __("Nueva Planta") }} </h1>
</div>
<div class="row">
<form method="POST" action="../planta" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label for="nombre" class="col-md-12 control-label"> {{ __("Nombre") }}
</label>
<input id="nombre" class="form-control" name="nombre" value="{{ old('nombre') }}"/>
</div>
<div class="form-group">
<label for="tamaño" class="col-md-12 control-label"> {{ __("Tamaño") }}
</label>
<input id="tamaño" class="form-control" name="tamaño" value="{{ old('tamaño') }}"/>
</div>
<div class="form-group">
<label for="flor" class="col-md-12 control-label"> {{ __("Flor") }}
</label>
<input id="flor" class="form-control" name="flor" value="{{ old('flor') }}"/>
</div>
<div class="form-group">
<label for="hoja" class="col-md-12 control-label"> {{ __("Hoja") }}
</label>
<input id="hoja" class="form-control" name="hoja" value="{{ old('hoja') }}"/>
</div>
<div class="form-group">
<label for="descripcion" class="col-md-12 control-label"> {{ __("Descripción") }}
</label>
<input id="descripcion" class="form-control" name="descripcion" value="{{ old('descripcion') }}"/>
</div>
<label class="btn btn-warning" for="foto">
<input id="foto" name="foto" type="file" style="display:none;"> {{ __("Subir imagen (Opcional)") }}
</label>
<button type="submit" name="addPlanta" class="btn btn-default"> {{ __("Añadir Planta") }}
</button>
</form>
</div>
@else
<h1 class="text-center text-mute" style="color:#FF0000"> {{ __("Insistimos, ¡INICIA SESIÓN SI QUIERES INTRODUCIR NUEVAS PLANTAS!") }} </h1>
@endLogged
@endsection
But the story is that I run the form, I'm going to phpMyAdmin to see the new plant and I got everything with this:
And ovbiamente this is anything but successful. In fact, I modify the variable cited by girasol.jpg (which is in public) and I find that now if you see the image.
How do I fix this? Apart, I'm calling images stored in public, but I will not always have the image stored right there, so one of 2, or I get the image to be searched from an absolute path or in case it has not imported from the directory public the image that generates a copy of the image in that directory (and I see this last recommended).
And true, the view I use:
@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(empty($plantas->foto)==false)
<img src="{{ $plantas->foto }}" style="width: 200px; height: 200px; border: 2px solid green" 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>
@endsection
Edit: I expand the information to show more data.
filesystems.php:
'planta' => [
'driver' => 'local',
'root' => storage_path('public\images\planta'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
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;
}
}
Edit: Try removing the fragment of the form for the image style="display: none;"
As seen in the image, the form can see the name of the image. If when this variable is stored in my phpMyAdmin database keep the name with which it shows, I would have all this solved, but guess what ...
Something goes wrong and is stored under another name.
Another thing, in the store () function of plantsController.php I tried to put this:
dd($request->foto);
Which produces the following result:
If I could somehow make what is in the underlined variable what the database stored, I would solve this. In fact, the function with which the original name is obtained is $ request- > foto-> gtClientOriginalName (), although it is information that tends to be lost after making the form.