Save form data to the database

2

I'm having a hard time trying to create a form in Laravel 5.4 . The form appears normal, but when I click the button, the page reloads and the data is not saved in the database!

{{ Form::open(array('url' => 'users')) }}
<div class="form-group">
    {{ Form::label('name', 'Name') }}
    {{ Form::text('name', null, array('class' => 'form-control')) }}
</div>

<div class="form-group">
    {{ Form::label('email', 'Email') }}
    {{ Form::email('email', null, array('class' => 'form-control')) }}
</div>

<div class="form-group">
    {{ Form::label('password', 'Senha') }}
    {{ Form::password('password', array('class' => 'form-control')) }}
</div>

<div class="form-group">
    {{ Form::label('nerd_level', 'User Level') }}
    {{ Form::select('nerd_level', array('0' => 'Nomal', '1' => 'Admin'), null, array('class' => 'form-control')) }}
</div>

{{ Form::submit('Create the User!', array('class' => 'btn btn-primary')) }{{ Form::close() }}

Controllers :

<?php namespace App\Http\Controllers;
 use Illuminate\Http\Request;
 use Illuminate\Routing\Controller as BaseController;
 use App\User;     
 use Illuminate\Support\Facades\Input;
 use Illuminate\Support\Facades\Validator;
 use Session;
 use Illuminate\Support\Facades\Redirect;
 class UserController extends BaseController
 {
public function index()
{
    // get all the users
    $users = User::all();

    // load the view and pass the users
    return view('users.index')
        ->with('users', $users);
}
public function create()
{
    // load the create form (app/views/users/create.blade.php)
    return view('users.create');
}
public function store()
{
    // validate
    // read more on validation at http://laravel.com/docs/validation
    $rules = array(
        'name'       => 'required',
        'email'      => 'required|email',
        'password'   => 'required|min:6|confirmed',
    );
    $validator = Validator::make(Input::all(), $rules);
    // process the login
    if ($validator->fails()) {
        return Redirect::to('users/create')
            ->withErrors($validator)
            ->withInput(Input::except('password'));
    } else {
        // store
        $user = new User;
        $user->name       =  Input::get('name');
        $user->email      = Input::get('email');
        $user->password   = bcrypt('password');
        $user->save();
        // redirect
        Session::flash('message', 'Usuario cadastrado com sucesso!');
        return Redirect::to('users');
    }
  }
 }

Routes :

Route::get('/', function () {
return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index');


Route::group(['middleware' => ['auth', 'auth.admin']], function () {
  // Minhas rotas da administração aqui
Route::resource('users', 'UserController');
Route::get('/admin', 'AdminController@index');
});

Could anyone help me?

    
asked by anonymous 24.05.2017 / 14:30

1 answer

1

No form::open within array setting of key url put like this:

{{ Form::open(array('url' => '/users/store')) }}

or by setting the key route by passing the route name:

{{ Form::open(array('route' => array('users.store'))) }}

This is the resource route you created in route settings, the problem that was previously going to the index route, so nothing happened.

The resource routing tables of follow one nomenclature and given as an example below:

Actions Handled By Resource Controller

+--------+-------------------------+---------+----------------+
|  Verb  |           Path          | Action  |    Route Name  |
+--------+-------------------------+---------+----------------+
| GET    | /photo                  | index   | photo.index    |
| GET    | /photo/create           | create  | photo.create   |
| POST   | /photo                  | store   | photo.store    |
| GET    | /photo/{photo}          | show    | photo.show     |
| GET    | /photo/{photo}/edit     | edit    | photo.edit     |
| PUT    | /PATCH/photo/{photo}    | update  | photo.update   |
| DELETE | /photo/{photo}          | destroy | photo.destroy  | 
+--------+-------------------------+---------+----------------+

where photo would be the name given by you on routes resource of a given Controller .

24.05.2017 / 20:27