php 如何将数据插入到 Laravel 数据库中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26067065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:07:44  来源:igfitidea点击:

How can I insert data into Database Laravel?

phplaravellaravel-4

提问by MD. Atiqur Rahman

I'm new to programming. I have created a basic form inside views/register.blade.php like this

我是编程新手。我在 views/register.blade.php 中创建了一个基本表单,如下所示

         @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();
        } 

Here is my route:

这是我的路线:

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?

我无法通过表单注册用户。你能建议我怎么做吗?

回答by Antonio Carlos Ribeiro

The error MethodNotAllowedHttpException means the route exists, but the HTTP method (GET) is wrong. You have to change it to POST:

错误 MethodNotAllowedHttpException 表示路由存在,但 HTTP 方法 (GET) 错误。您必须将其更改为 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();
}

And I removed the line:

我删除了这一行:

$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.

它将显示输入中的所有字段。

回答by Carlos

make sure you use the POSTto insert the data. Actually you were using GET.

确保使用POST插入数据。实际上你正在使用GET

回答by Nikunj K.

First method you can try this

第一种方法你可以试试这个

$department->department_name = $request->department_name;
$department->status = $request->status;
$department->save();

Another way to insert records into the database with create function

使用 create 函数将记录插入数据库的另一种方法

$department = new Department;           
// Another Way to insert records
$department->create($request->all());

return redirect('admin/departments');

You need to set the filledby in Department model

您需要在部门模型中设置fillby

namespace App;

use Illuminate\Database\Eloquent\Model;

class Department extends Model
{
    protected $fillable = ['department_name','status'];
}