Save form data to database

Asked

Viewed 590 times

2

I’m having a hard time trying to create a form on 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 someone help me?

  • Put routes and controller to your question...

1 answer

1


In the form::open within the array key setting url put it like this:

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

or by key configuration route passing the name of the route:

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

which is the route resource created by you in the route settings, the problem that previously is going to the route index and so nothing happened.


The Resource route creation tables follows a 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 the routes resource of a particular Controller.

References:

  • 1

    Now it worked out! Thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.