php 函数存储未保存在数据库laravel 8中

g6ll5ycj  于 2023-02-28  发布在  PHP
关注(0)|答案(1)|浏览(212)

我正在使用laravel 8控制器保存文本输入类型使用“存储功能”。我的输入为'名称'没有存储在数据库中时,我提交了它。
这是我的刀

<form class="text-center  p-4" action="{{ route('store') }}" method = "POST">
@csrf

<div class="mb-3">
 <label for="exampleFormControlInput1" class="form-label">names</label>
 <input type="text" class="form-control" id="name" placeholder="name">
</div>

 <button class="btn btn-primary" type="submit">Button</button>
</form>

这是我的产品控制器. php

public function create()

{

return view('products.create');

}

public function store(Request $request)
{
 $request->validate([

'name' => 'required',
'detail' => 'required',

 ]);

 Product::create($request->all());
 $products -> save();

 return redirect('/saving-list') ->with('success','Umrah record has been updated');

 }
eaf3rand

eaf3rand1#

您的参数不完整,'detail' => 'required'但未找到
你需要新的值输入<input type="text" class="form-control" id="detail" placeholder="detail" name="detail">
并且必须在每个输入中添加属性name
获取如下所示的完整代码

<form class="text-center  p-4" action="{{ route('store') }}" method = "POST">
@csrf

<div class="mb-3">
 <label for="exampleFormControlInput1" class="form-label">names</label>
 <input type="text" class="form-control" id="name" name="name" placeholder="name">
 <input type="text" class="form-control" id="detail" name="detail" placeholder="detail">
</div>

 <button class="btn btn-primary" type="submit">Button</button>
</form>

保存或存储方法请访问https://laravel.com/docs/8.x/eloquent#inserts

$product= new Product;
$product->name = $request->name;
$product->detail= $request->detail;
$product->save();

或者像这样

$product= Product::create([
    'name' => $request->name,
    'detail' => $request->detail,
]);

相关问题