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...
– novic