在laravel中保存表单数据 [英] Save form data in laravel
问题描述
现在我有一个带有性别,选项和user_id的表格.
我的public function store (Request $request)
看起来像这样:
Right now I have a form with gender, options and user_id.
My public function store (Request $request)
looks like this :
public function store(Request $request)
{
$task = new Appointment;
$task->gender = $request->gender;
$task->options = $request->options;
$task->user_id = $request->user_id;
$task->save();
}
这完全可以正常工作,但这只是3个字段?! 最终,我希望表格大5倍.我的职能将是巨大的.有没有办法用更少的代码保存所有内容?
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?
我发现了这个:$data = Input::all();
这样可以获取所有数据,但是我不知道如何将其保存在数据库中.
I found this : $data = Input::all();
This gets all the data but I don't know how to save it in the database.
推荐答案
您可以使用质量使用create()
方法分配功能:
You can use mass assignment feature by using create()
method:
public function store(Request $request)
{
Appointment::create($request->all());
}
不要忘记在Appointment
模型的$fillable
数组中填充所有列:
Don't forget to fill all columns in $fillable
array in Appointment
model:
protected $fillable = ['gender', 'options', 'user_id', 'another_one'];
这篇关于在laravel中保存表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!