如何将请求对象从表单传递到 PHP

我正在使用 Laravel 5.7 开发 Laravel 应用程序,但遇到以下问题:

  1. 我正在将请求对象“req”从控制器传递给视图。

  2. 在视图中,我显示了传递给视图的请求对象的内容

  3. 我在同一个视图中有一个表单,它假设将相同的请求对象“req”转发到控制器,在那里我用它做一些处理,但不知道如何发送整个请求对象而不是发送单个元素。

我想做这样的事情

<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>


Cats萌萌
浏览 130回答 1
1回答

MMTTMM

从表单提交的数据就是请求。你也错过了一些引号。<form method="post" action="/story/editorsubmit" enctype="multipart/form-data">&nbsp; &nbsp; @csrf&nbsp; &nbsp; <input type="hidden" name="fullobject" value="{{ $req }}">&nbsp; &nbsp; <button type="submit" name="submitButton" value="edit" class="btn btn-primary">Edit</button></form>可以使用提交的属性名称在表单提交的控制器中访问您当前的数据。但我怀疑这会起作用,因为 $req 是一个对象,而不是一个字符串。$object = request('fullobject');但理想情况下,您应该单独定义属性。我假设这些隐藏元素实际上代表可编辑的表单输入?如果没有任何变化,那么这样做是没有意义的。编辑:添加了处理数组的方法。<form method="post" action="/story/editorsubmit" enctype="multipart/form-data">&nbsp; &nbsp; @csrf&nbsp; &nbsp; @foreach ($req->all() as $key => $value)&nbsp; &nbsp; &nbsp; &nbsp; @if (is_array($value))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach($value as $v)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" name="{{ $key }}[]" value="{{ $v }}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; &nbsp; &nbsp; @else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" name="{{ $key }}" value='{{ $value }}'>&nbsp; &nbsp; &nbsp; &nbsp; @endif&nbsp; &nbsp; @endforeach&nbsp; &nbsp; <button type="submit" name="submitButton" value="edit" class="btn btn-primary">Edit</button></form>然后提交的请求将像以前一样包含各个属性。
打开App,查看更多内容
随时随地看视频慕课网APP