Problems uploading an image

0

You see, I have the table Game:

Schema::create('juegos', function (Blueprint $table){
        $table->increments('id');
        $table->integer('numero')->unique();
        $table->string('nombre');
        $table->unsignedInteger('agrupacion_id');
        $table->foreign('agrupacion_id')->references('id')->on('agrupacions');
        $table->text('materiales');
        $table->text('organizacion');
        $table->text('desarrollo');
        $table->string('foto');
        $table->text('observaciones');
        $table->text('variantes');
        $table->timestamps();
    });

For this I have the following form:

@extends('layouts.app')
@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Cree un nuevo juego</div>

                <div class="card-body">
                    <form method="POST" action="nuevo_juego" novalidate>
                        @csrf

                        <div class="form-group row">
                            <label for="numero" class="col-md-4 col-form-label text-md-right">Código del juego</label>

                            <div class="col-md-6">
                                <input id="numero" type="number" class="form-control{{ $errors->has('numero') ? ' is-invalid' : '' }}" name="numero" value="{{ old('numero') }}" required autofocus>

                                @if ($errors->has('numero'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('numero') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="nombre" class="col-md-4 col-form-label text-md-right">Nombre del juego</label>

                            <div class="col-md-6">
                                <input id="nombre" type="text" class="form-control{{ $errors->has('nombre') ? ' is-invalid' : '' }}" name="nombre" value="{{ old('nombre') }}" required autofocus>

                                @if ($errors->has('nombre'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('nombre') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="agrupacion" class="col-md-4 col-form-label text-md-right">¿Como se organizaran los equipos?</label>

                            <div class="col-md-6">
                                <select id="agrupacion" type="text" class="form-control{{ $errors->has('agrupacion') ? ' is-invalid' : '' }}" name="agrupacion" required autofocus>
                                    @foreach($agrupaciones as $agrupar)
                                        <option value="{{$agrupar->id}}" {{(old('agrupacion') == $agrupar->id) ? 'selected' : ''}}>{{$agrupar->nombre}}</option>
                                    @endforeach
                                </select>

                                @if ($errors->has('agrupacion'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('agrupacion') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="materiales" class="col-md-4 col-form-label text-md-right">Materiales que se utilizaran en el juego</label>

                            <div class="col-md-6">
                                <textarea id="materiales" name="materiales">{{ old('materiales') }}</textarea>

                                @if ($errors->has('materiales'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('materiales') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="organizacion" class="col-md-4 col-form-label text-md-right">Como se organizara el juego</label>

                            <div class="col-md-6">
                                <textarea id="organizacion" name="organizacion">{{ old('organizacion') }}</textarea>

                                @if ($errors->has('organizacion'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('organizacion') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="desarrollo" class="col-md-4 col-form-label text-md-right">Desarrollo del juego</label>

                            <div class="col-md-6">
                                <textarea id="desarrollo" name="desarrollo">{{ old('desarrollo') }}</textarea>

                                @if ($errors->has('desarrollo'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('desarrollo') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="observaciones" class="col-md-4 col-form-label text-md-right">Reglas y observaciones</label>

                            <div class="col-md-6">
                                <textarea id="observaciones" name="observaciones">{{ old('observaciones') }}</textarea>

                                @if ($errors->has('observaciones'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('observaciones') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="Variantes" class="col-md-4 col-form-label text-md-right">Variantes</label>

                            <div class="col-md-6">
                                <textarea id="variantes" name="variantes">{{ old('variantes') }}</textarea>

                                @if ($errors->has('variantes'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('variantes') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <hr>

                        <div style="margin-left: 35%;">
                            <b><i><u>Habilidades requeridas:</u></i></b><br><br>
                            @foreach($contenidos as $contenido)
                                <input type="checkbox" name="capacidad" value="{{$contenido->id}}"> {{$contenido->nombre}}<br>
                            @endforeach
                        </div>

                        @if ($errors->has('capacidad'))
                            <span class="invalid-feedback">
                                <strong>{{ $errors->first('capacidad') }}</strong>
                            </span>
                        @endif

                        <hr>

                        <div class="form-group row">
                            <label for="foto" class="col-md-4 col-form-label text-md-right">Imagen del juego</label>

                            <div class="col-md-7">
                                <input id="foto" type="file" class="form-control{{ $errors->has('foto') ? ' is-invalid' : '' }}" name="foto" value="{{ old('foto') }}" />

                                @if ($errors->has('foto'))
                                    <span class="invalid-feedback">
                                        <strong>{{ $errors->first('foto') }}</strong>
                                    </span>
                                @endif
                            </div>
                        </div>

                        <div class="form-group row mb-0">
                            <div class="col-md-6 offset-md-4">
                                <button type="submit" class="btn btn-primary">
                                    Crear Juego
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
@endsection

And it is managed by JuegoRequest:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class JuegoRequest extends FormRequest{
    public function authorize(){
        return true;
    }

    public function messages(){
        return[
            'numero.required'=>'El número del juego es obligatorio',
            'numero.integer'=>'Solo se admiten valores numéricos',
            'numero.min'=>'No se admiten números negativos',
            'numero.unique'=>'Ese número ya estaba repetido',
            'nombre.required'=>'El nombre del juego es obligatorio',
            'materiales.required'=>'Es obligatorio indicar que materiales se utilizan',
            'organizacion.required'=>'Es obligatorio indicar la organicación de los jugadores y los demas componentes del juego',
            'desarrollo.required'=>'Es obligatorio indicar como se desarrolla el juego',
            'foto.required'=>'Debes añadir una imagen que represente el juego',

            'observaciones.required'=>'Es obligatorio indicar las observaciones acerca del juego'
        ];
    }

    public function rules(){
        return[
            'numero'=>'required|integer|min:0|unique:juegos',
            'nombre'=>'required',
            'materiales'=>'required',
            'organizacion'=>'required',
            'desarrollo'=>'required',
            'foto'=>'required',
            'observaciones'=>'required'
        ];
    }
}

And it goes through this function:

public function sumar(JuegoRequest $request){
    if(empty($request->variantes))
        $c=null;
    else
        $c=$request->variantes;

     $nombre=str_random(30).'-'.$request->file('foto')->getClientOriginalName();
     $request->file('foto')->move('storage',$nombre);

    $a=Juego::create([
        'numero'=>$request->numero,
        'agrupacion_id'=>$request->agrupacion,
        'nombre'=>$request->nombre,
        'materiales'=>$request->materiales,
        'organizacion'=>$request->organizacion,
        'desarrollo'=>$request->desarrollo,
        'observaciones'=>$request->observaciones,
        'variantes'=>$c,
        'foto'=>$nombre
    ]);

    return back()->with('message',['success','Juego creado con exito.']);
}

But I get this error message:

It's a nightmare to include images in Laravel, so I would appreciate it if someone tells me what I'm doing wrong.

More information: I made this modification in JuegoRequest:

'foto'=>'required|image'

This will make that when entering the data it is verified that the photo is an image, but I miss error. I also have a function to assign a photo to the user and that works correctly.

    
asked by Miguel Alparez 11.07.2018 в 16:10
source

1 answer

1

you must first include the following line in the form

<form method="POST" action="subir" accept-charset="UTF-8" enctype="multipart/form-data">

in the controller part I do it in the following way

public function sumar(JuegoRequest $request){
// en la estrutura lo tengo de la suiente forma sumar(Request $request, $id) 

    if(empty($request->variantes))
        $c=null;
    else
        $c=$request->variantes;

    $file2 = Input::file('foto');//foto corresponde el name de input
    if (isset($file2)){
    //$nombres = $file2->getClientOriginalName();
    $nombres = time().str_random(5).'.'.$file2->getClientOriginalExtension();
    \Storage::disk('local')->put($nombres,  \File::get($file2));
    $nombres;//nombre de archivo
    }


    $a=Juego::create([
            'numero'=>$request->numero,


 'agrupacion_id'=>$request->agrupacion,
        'nombre'=>$request->nombre,
        'materiales'=>$request->materiales,
        'organizacion'=>$request->organizacion,
        'desarrollo'=>$request->desarrollo,
        'observaciones'=>$request->observaciones,
        'variantes'=>$c,
        'foto'=>$nombres
    ]);



   return back()->with('message',['success','Juego creado con exito.']);
}

the file is saved in \ storage \ app

to access the file in the application you have to place in the router / web.php

Route::get('foto_deporte/{file}', function ($file) {
    return Storage::disk('local')->response("$file");

and in the view

<img class="img-circle" src="{{ asset("foto_deporte/$foto")  }}" alt="User Image">
    
answered by 11.07.2018 / 17:16
source