加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

php – 在laravel中保存表单数据

发布时间:2020-12-14 19:46:46 所属栏目:大数据 来源:网络整理
导读:现在我有一个包含gender,options和user_id的表单. 我的公共函数存储(Request $request)如下所示: public function store(Request $request){ $task = new Appointment; $task-gender = $request-gender; $task-options = $request-options; $task-user_id =
现在我有一个包含gender,options和user_id的表单.
我的公共函数存储(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();
}

这完全没问题,但这只是3个字段?
最终我希望我的表格大5倍.我的功能将是巨大的.有没有办法用更少的代码保存一切?

我发现了这个:$data = Input :: all();
这将获取所有数据,但我不知道如何将其保存在数据库中.

您可以使用create()方法使用 mass assignment功能:
public function store(Request $request)
{
    Appointment::create($request->all());
}

不要忘记在Appointment模型中填充$fillable数组中的所有列:

protected $fillable = ['gender','options','user_id','another_one'];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读