php 在laravel中保存表单数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39436164/
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
Save form data in laravel
提问by twoam
Right now I have a form with gender, options and user_id.
My public function store (Request $request)
looks like this :
现在我有一个包含性别、选项和 user_id 的表单。我public function store (Request $request)
看起来像这样:
public function store(Request $request)
{
$task = new Appointment;
$task->gender = $request->gender;
$task->options = $request->options;
$task->user_id = $request->user_id;
$task->save();
}
This works completely fine but this is just 3 fields ?! Eventually I want my forms 5 times bigger. My function will be huge. Is there a way to save everything with less code?
这完全正常,但这只是 3 个字段?!最终我希望我的表格大 5 倍。我的功能将是巨大的。有没有办法用更少的代码保存所有内容?
I found this : $data = Input::all();
This gets all the data but I don't know how to save it in the database.
我发现了这一点:$data = Input::all();
这会获取所有数据,但我不知道如何将其保存在数据库中。
回答by Alexey Mezenin
You can use mass assignmentfeature by using create()
method:
您可以使用以下方法使用质量分配功能create()
:
public function store(Request $request)
{
Appointment::create($request->all());
}
Don't forget to fill all columns in $fillable
array in Appointment
model:
不要忘记$fillable
在Appointment
模型中填充数组中的所有列:
protected $fillable = ['gender', 'options', 'user_id', 'another_one'];