简体   繁体   中英

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:

  1. I am passing a request object "req" from controller to view.
  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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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