It turns out that I am trying to send data, in Laravel , through a form using Laravel Collective . But when I press the Register button (the one that starts the request to send the data) nothing happens. Checking the console, it shows me that the form is sent to the method create
of the controller and not to the method store
as it should be. I am using Laravel 5.3 and version 5.2 of Laravel Collective (the PHP version of the server is 5.5 , and it does not let me update to the latest version of the form). I hope you can help me.
@extends('layouts.main')
@section('title' ,'Agregar Evento')
@section('content')
<div class="container">
<div class="panel panel-default">
<div class="panel-heading"> <h3 class="panel-title"> Nuevo Evento </h3> </div>
<div class="panel-body">
{!! Form::open(['route' => 'agenda.store' , 'id' => 'formAgregarEvento' , 'name' => 'formAgregarEvento']) !!}
<div class="form-group">
{!! Form::label('name' , 'Nombre'); !!}
{!! Form::text('name' , null, ['class' =>'form-control', 'placeholder' => 'Nombre evento' , 'required' ]); !!}
</div>
<div class="form-group">
{!! Form::label('lugar' , 'Lugar'); !!}
{!! Form::text('lugar' , null, ['class' =>'form-control', 'placeholder' => 'Lugar del evento' , 'required' ]); !!}
</div>
<div class="form-group">
{!! Form::label('url' , 'URL') !!}
{!! Form::text('url' , null, ['class' =>'form-control', 'placeholder' => 'Ingrese URL' , 'required' ]); !!}
</div>
<div class="form-group">
{!! Form::label('FechaInicio' , 'Fecha de Inicio'); !!}
{!! Form::date('fechaInicio'); !!}
</div>
<div class="form-group">
{!! Form::label('FechaTermino' , 'Fecha de Término'); !!}
{!! Form::date('fechaTermino'); !!}
</div>
<div class="form-group">
{!! Form::label('descripcion' , 'Descripción'); !!}
{!! Form::textarea('descripcion' , null, ['class' =>'form-control', 'placeholder' => 'Descripción del evento' , 'required' ]); !!}
</div>
<div class="form-group">
<button id="registrarBtn" class="btn btn-primary" type="submit"> Registrar </buttton>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
@endsection
Routes:
Route::get('/admin/agenda', 'EventoController@index'); // un evento se considera una agenda
Route::resource('agenda', 'EventoController');
Route::resource('informaciones', 'InformacionesController');
Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Models\Evento;
use DB;
class EventoController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(){
$eventos = \App\Models\Evento::all();
return view('agenda.index', ['eventos' => $eventos])->render();
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('agenda.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
$evento = new \App\Models\Evento($request->all());
$evento->save();
}
EDIT: Finally I did not keep using the Laravel Collective form. Add the inputs and the form in the "classic" form. I also made an Ajax call, using jQuery, to access the driver and insert the data. I leave the code
jQuery:
$("#registrarBtn").click(function(e){
e.preventDefault();
var dataForm = $("#formAgregarEvento").serialize();
$.post('/agenda' , dataForm , function(e){
alert('Evento Agregado');
});
Store Driver:
public function store(Request $request)
{
//
DB::table('eventos')->insert([
'titulo' => $request->input('titulo'),
'descripcion' => $request->input('descripcion'),
'lugar' => $request->input('lugar'),
'url' => $request->input('url'),
'fechaInicio' => $request->input('fechaInicio'),
'fechaTermino' => $request->input('fechaTermino')
]);
return view('agenda.create');
}