简体   繁体   English

如何将请求对象从表单传递给Php

[英]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应用程序,但我遇到以下问题:

  1. I am passing a request object "req" from controller to view. 我正在从控制器传递请求对象“ req”以进行查看。
  2. In view I show the contents of request object passed to view 在视图中,我显示了传递给视图的请求对象的内容
  3. I have a form in the same view which is suppose to forward the same request object "req" to a controller where I do some processing with it but can't figure out how can I send the whole request object rather than sending individual elements. 我在同一视图中有一个表单,该表单假定将相同的请求对象“ req”转发到控制器,在控制器上我对其进行一些处理,但无法弄清楚如何发送整个请求对象而不是发送单个元素。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM