[英]How to pass Request Object from a form to Php
I am developing a Laravel application using Laravel 5.7 and I have following problem: 我正在使用Laravel 5.7开发Laravel应用程序,但我遇到以下问题:
I want to do something like this 我想做这样的事情
<form method="post" action="/story/editorsubmit" enctype="multipart/form-data">
@csrf
<input type="hidden" name="fullobject" value={{ $req }}>
<button type="submit" name="submitButton" value="edit" class="btn btn-primary">Edit</button>
</form>
The data submitted from the form is what becomes the request. 从表单提交的数据成为请求。 Also you were missing some quotes.
您也缺少一些报价。
<form method="post" action="/story/editorsubmit" enctype="multipart/form-data">
@csrf
<input type="hidden" name="fullobject" value="{{ $req }}">
<button type="submit" name="submitButton" value="edit" class="btn btn-primary">Edit</button>
</form>
Your current data could be accessed in the controller on form submit using the name of the submitted property. 您可以使用提交属性的名称在提交表单的控制器中访问当前数据。 But I doubt this would work as $req is an object, not a string.
但是我怀疑这是否可以工作,因为$ req是对象,而不是字符串。
$object = request('fullobject');
But ideally, you should be defining the properties separately. 但理想情况下,您应该分别定义属性。 I'm assuming these hidden elements actually represent editable form inputs?
我假设这些隐藏元素实际上代表可编辑的表单输入? If nothing is changing, it doesn't make sense to do it this way.
如果什么都没有改变,那么这样做就没有意义了。
EDIT: Added way of dealing with arrays. 编辑:添加了处理数组的方法。
<form method="post" action="/story/editorsubmit" enctype="multipart/form-data">
@csrf
@foreach ($req->all() as $key => $value)
@if (is_array($value))
@foreach($value as $v)
<input type="hidden" name="{{ $key }}[]" value="{{ $v }}">
@endforeach
@else
<input type="hidden" name="{{ $key }}" value='{{ $value }}'>
@endif
@endforeach
<button type="submit" name="submitButton" value="edit" class="btn btn-primary">Edit</button>
</form>
Then the submitted request would contain the individual properties like before. 然后,提交的请求将像以前一样包含各个属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.