Foreign variable is not captured by form

0

See, I have this table:

    Schema::create('fotos', function(Blueprint $table){
        $table->increments('id');
        $table->string('foto');
        $table->string('nombre');
        $table->date('fecha');
        $table->unsignedInteger('user_id'); // Foraneo a la tabla User.
        $table->foreign('user_id')->references('id')->on('users');
        $table->timestamps();
    });

And I have this 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"><h1>Subir nueva imagen</h1></div>

                    <div class="card-body">
                        <form method="POST" action="subir_foto" enctype="multipart/form-data">
                            @csrf

                            <input id="user_id" type="hidden" name="user_id" value="{{ auth()->user()->id }}">

                            <div class="form-group row">
                                <label for="foto" class="col-md-4 col-form-label text-md-right">Foto</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">
                                <label for="nombre" class="col-md-4 col-form-label text-md-right">Nombre</label>

                                <div class="col-md-6">
                                    <input id="nombre" type="text" class="form-control{{ $errors->has('nombre') ? ' is-invalid' : '' }}" name="nombre" value="{{ old('nombre') }}" placeholder="Expliqua donde la sacaste o en que consiste" 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="fecha" class="col-md-4 col-form-label text-md-right">Fecha en la que se creo la foto</label>

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

                                    @if ($errors->has('fecha'))
                                        <span class="invalid-feedback">
                                            <strong>{{ $errors->first('fecha') }}</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">
                                        Subir Foto
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

But I get this error:

  

SQLSTATE [HY000]: General error: 1364 Field 'user_id' does not have a   default value (SQL: insert into fotos ( fecha , nombre , foto ,    updated_at , created_at ) values (2018-11-07, XCOM2,   lOy6Tw0NpryHVl7wtGSshxsipXLlN7xcom.jpg, 2018-11-07 10:17:43,   2018-11-07 10:17:43))

What have you done wrong? I also have this in Foto.php:

protected $fillable=['foto','nombre','fecha','user_id'];
    
asked by Miguel Alparez 07.11.2018 в 11:19
source

1 answer

2

I have already solved it. It turns out that when I entered the photo in the table, I forgot that it did not enter all the variables at the stroke with $request->all() , but manually entered the variables. What I have done is to remove the "input hidden" and add the variable in the function to create the row:

public function sumar(FotoRequest $request){
    $foto=str_random(30).$request->file('foto')->getClientOriginalName();
    $request->file('foto')->move('archivo',$foto);
    $a=Foto::create([
        'fecha'=>$request->fecha,
        'nombre'=>$request->nombre,
        'foto'=>$foto,
        'user_id'=>auth()->user()->id
    ]);
    return back()->with('message',['success','Foto introducida con éxito. ¿Vas a subir otra foto?']);
}
    
answered by 07.11.2018 в 12:25