laravel 使用laravel将数据插入数据库的最简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25494330/
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
Simplest way to insert data into database with laravel?
提问by Edin Sita
what is simples way to insert data into database using laravel framework
什么是使用 Laravel 框架将数据插入数据库的简单方法
have this form:
有这个表格:
<div class="border">
{{ Form::open(array('url' => 'menu/profil', 'files' => true)) }}
{{ Form::text('username') }}
{{ Form::submit('submit') }}
{{ Form::close() }}
</div>
and this
和这个
Route::post('menu/profil', function() {
$username = Input::get('username');
//code to insert username into database
});
回答by
First, create a Model for your table:
首先,为您的表创建一个模型:
/* app/models/User.php */
class User extends Eloquent {
protected $table = 'my_users';
}
Second, insert data by instantiating your Model:
其次,通过实例化您的模型插入数据:
$user = new User;
$user->username = Input::get('username');
$user->save();
For more information check documentation
有关更多信息,请查看文档
回答by user1669496
Assuming you have your user model setup...
假设你有你的用户模型设置......
User::create(array('username' => Input::get('username')));