Error creating a table by _token

1

I have a User table and an Entry table, which is for a user to buy an entry to visit a botanical garden.

Ticket data:

Schema::create('entradas', function (Blueprint $table){
            $table->increments('id');
            $table->date('fecha');
            $table->unsignedInteger('persona'); // Quien ha comprado la entrada.
            $table->foreign('persona')->references('id')->on('users');
            $table->timestamps();
        });

We must also bear in mind that the User table will have a variable 'balance', which will indicate how much money that person has left. Buying a ticket costs € 7.

Form to buy the ticket:

@extends('layouts.app')
@section('content')

@Logged()
@include('partials.errors')
<div align="center" class="panel panel-default">
    <h1 class="text-center text-mute"> {{ __("Nueva reserva") }} </h1>
    <h2>Recuerda que una entrada son 7€<br>Tienes actualmente {{Auth::user()->saldo}}€</h2>
</div>
<div class="row">
    <form method="POST" action="../reserva">
        {{ csrf_field() }}
        <div class="form-group">
            <label for="fecha" class="col-md-12 control-label"> {{ __("Indique la fecha") }}
            </label>
            <input id="fecha" style="width:150px" type="date" class="form-control" name="fecha" value="{{ old('fecha') }}"/>
        </div>

        <button type="submit" name="addPlanta" class="btn btn-default"> {{ __("Reservar entrada") }}
        </button>
    </form>
</div>
@else
    <h1 class="text-center text-mute" style="color:#FF0000"> {{ __("Debes haber iniciado sesión para crear una reserva") }} </h1>
@endLogged
@endsection

Once filled out, we go to the following web.php site:

Route::post('reserva','EntradaController@confirmar');

And this brings us to the next function:

public function confirmar(EntradaRequest $r){
        if(Auth::user()->saldo<7)
            return back()->with('message', ['msg', __('No tienes suficiente dinero')]);
        elseif($r->fecha>=now()->toDateString()){
            $r->merge(['persona' => auth()->id()]);
            Entrada::create($r->input());
            Auth::user()->saldo-=7;
            Auth::user()->save();
            return back()->with('message', ['success', __('Reserva realizada con exito. Se han descontado 7€ de tu saldo')]);
        }
        return back()->with('message', ['msg', __('No puede reservar para una fecha pasada')]);
    }

During this function, InputRequest.php is used, which has this code:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class EntradaRequest extends FormRequest{
    public function authorize()    {
        return auth()->check();
    }
    public function rules(){
        return [
            'fecha' => 'required',
        ];
    }
}

But when trying to execute the questionnaire, I run into this:

I have verified that the error is concretely in Entrada::create($r->input());

    
asked by Miguel Alparez 18.02.2018 в 16:18
source

1 answer

2

First of all in your model Entrada you should have assigned in the array $fillable the columns of your table that you will assign massively as is the case of the method create

protected $fillable = ['fecha', 'persona'];

Secondly in your controller and the FormRequest , (InputRequest) returns a array with the data if they passed the corresponding validations. Then you should access $r->all() and switch to the Create method, but you also want to add the person manually, you could do so by adding that property with + and Ojo the id of the user logueado you get different.

elseif($r->fecha>=now()->toDateString()){
        Entrada::create($r->all() +  ['persona' => auth()->user()->id()]);
        Auth::user()->saldo-=7;
        Auth::user()->save();
        return back()->with('message', ['success', __('Reserva realizada con exito. Se han descontado 7€ de tu saldo')]);
 }
    
answered by 18.02.2018 / 16:36
source