如何将数据插入数据库Laravel? [英] How can I insert data into Database Laravel?
问题描述
我是编程新手.我已经在views/register.blade.php内部创建了一个基本表单
I'm new to programming. I have created a basic form inside views/register.blade.php like this
@section('content')
<h1>Registration Form</h1><hr>
<h3>Please insert the informations bellow:</h3>
{{Form::open(array('url'=>'test/register','method'=>'post'))}}
<input type="text" name="username" placeholder="username"><br><br>
<input type="text" name="email" placeholder="email"><br><br>
<input type="password" placeholder="password"><br><br>
<input type="submit" value="REGISTER NOW!">
{{Form::close()}}@stop
我有一个控制器.像这样
I have a controller. like this
public function create()
{
$user= Input::all();
$user = new User;
$user->username = Input::get('username');
$user->email = Input::get('email');
$user->password = Input::get('password');
$user->save();
return Redirect::back();
}
这是我的路线:
Route::get('test/register', array('uses'=>'TestController@create'))
我无法通过表单注册用户.您能建议我该怎么做吗?
I can not register users through the form. Would you please suggest me how to do that?
推荐答案
错误MethodNotAllowedHttpException表示路由存在,但是HTTP方法(GET)错误.您必须将其更改为POST:
The error MethodNotAllowedHttpException means the route exists, but the HTTP method (GET) is wrong. You have to change it to POST:
Route::post('test/register', array('uses'=>'TestController@create'));
此外,您需要对密码进行哈希处理:
Also, you need to hash your passwords:
public function create()
{
$user = new User;
$user->username = Input::get('username');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->save();
return Redirect::back();
}
我删除了这一行:
$user= Input::all();
因为在下一个命令中您将其内容替换为
Because in the next command you replace its contents with
$user = new User;
要调试输入,可以在控制器的第一行:
To debug your Input, you can, in the first line of your controller:
dd( Input::all() );
它将显示输入中的所有字段.
It will display all fields in the input.
这篇关于如何将数据插入数据库Laravel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!