php Laravel:在模型表中添加新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22924605/
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 16:22:11  来源:igfitidea点击:

Laravel : add new row in model table

phplaravellaravel-4form-submit

提问by Tarik Mokafih

I have a user model defined, and I'm trying to add a new user in my database using a form, what is the best and fastest way to do it, I have read something about model forms binding but I think it's just for updating not for adding new rows.

我定义了一个用户模型,我正在尝试使用表单在我的数据库中添加一个新用户,最好和最快的方法是什么,我已经阅读了一些关于模型表单绑定的内容,但我认为它只是为了更新不是用于添加新行。

I'm new in Laravel and couldn't find some good tutorials, I must recognize that Laravel documentation is really poor, so any links to some good and well explained tutorials are welcomed. Thank you

我是 Laravel 的新手,找不到一些好的教程,我必须认识到 Laravel 文档真的很差,因此欢迎任何指向一些很好且解释清楚的教程的链接。谢谢

回答by The Alpha

Assumed that, you have a Usermodel (app/models/User.php) the one came with Laravelby default, which may look like this:

假设您有一个默认附带的User模型 ( app/models/User.php) Laravel,它可能如下所示:

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    protected $table = 'users';
    protected $hidden = array('password');

    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    public function getAuthPassword()
    {
        return $this->password;
    }

    public function getReminderEmail()
    {
        return $this->email;
    }
}

Now, from a controller (Basically) you may use somethng like this:

现在,从控制器(基本上)你可以使用这样的东西:

$user = new user;
$user->usaername = 'Me';
$user->email = '[email protected]';
// add more fields (all fields that users table contains without id)
$user->save();

There are other ways, for example:

还有其他方法,例如:

$userData = array('username' => 'Me', 'email' => '[email protected]');
User::create($userData);

Or this:

或这个:

User::create(Input::except('_token'));

This requires you to use a property in your Usermodel like this:

这要求您在User模型中使用这样的属性:

class User extends Eloquent implements UserInterface, RemindableInterface {

    protected $fillable = array('username', 'email');

    // Or this,  (Read the documentation first before you use it/mass assignment)
    protected $guarded = array();

}

Since, you are still new to Laravelyou may use first example and read about Mass Assignment, then you may use the second one if you want.

因为,您仍然是新手,Laravel您可以使用第一个示例并阅读Mass Assignment,然后您可以根据需要使用第二个示例。

Update:

更新:

In your controller, you may use Input::get('formfieldname')to get the submitted data, for example:

在您的控制器中,您可以使用Input::get('formfieldname')来获取提交的数据,例如:

$username = Input::get($username);

So, you can use these data like this:

因此,您可以像这样使用这些数据:

$user = new User;
$user->usaername = $username;

Or directly you can use:

或者直接使用:

$user->email = Input::get($email);
$user->save();

In the form, you have to set the form action, where you'll submit the form and in this case you have to declare a route, for example:

在表单中,您必须设置form action,您将在其中提交表单,在这种情况下,您必须声明一个路由,例如:

Route::post('user/add', array('as' => 'user.add', 'uses' => 'UserController@addUser'));

Then in your controller you have to create the method addUser, like this:

然后在您的控制器中,您必须创建方法addUser,如下所示:

class UserController extends addUser {

    // other methods

    public function addUser()
    {
        $user = new user;
        $user->username = Input::get('username');
        $user->email = Input::get($email);
        $user->save();
    }
}

In your form you may use this:

在你的表格中,你可以使用这个:

Form::open(array('route' => 'user.add'))

Read the documentationproperly, you can do it easily.

正确阅读文档,您可以轻松完成。