0

I have a simple structure:

       @for ($i = 1; $i <= $page_num; $i++) // $page_num = 37;
            <div class="divTableRow">
                <div class="divTableCell">{{$i}}</div>
                <input name="{{$i}}" value="{{$i}}" type="number"/>
            </div>
       @endfor

This is a controller:

public function store(Request $request)
{
    $bodyContent = $request->getContent();
    $obj = json_encode($bodyContent);
    dd($obj); // here I want to see, what I get
}

So I get such a thing:

"_token=VcSFPaYfdte9zb7Xa6c42vsxilwFnZ2hWLOxV&1=1&2=2&3=3&4=4&5=5&6=6&7=7&8=8&9=9&10=10&11=11&12=12&13=13&14=14&15=15&16=16&17=17&18=18&19=19&20=20&21=21&22=22&23=23&24=24&25=25&26=26&27=27&28=28&29=29&30=30&31=31&32=32&33=33&34=34&35=35&36=36&37=37 ◀"

Is it possible to transform it to json format after all?:

{
  "1" : "1",
  "2" : "2",
  "3" : "3",
  "4" : "4",
   .. : ..,
   .
   .
  "37" : "37"
} 

If yes, could someone point me the way to solve this issue? Later have to save this formated output to the file *.json, but for now it is not important. Searched for the Hints, but nothing helps, I get endless errors

4
  • use $request->query() to get the query string from request Commented Jan 28, 2019 at 19:35
  • didn't help, I get empty array [ ] Commented Jan 28, 2019 at 19:39
  • 2
    are you submitting form via post or get ? if its post then use request->all() Commented Jan 28, 2019 at 19:42
  • Thanks for your attention to this topic! Upvote. Commented Jan 28, 2019 at 19:49

1 Answer 1

3

You should use $request->all(); which return an array of all the input fields, then encoding that to json should give you your desired output.

So:

$obj = json_encode($request->all());

should be what you are looking for. And in order to exclude the token you can use:

$obj = json_encode($request->except('_token'));
Sign up to request clarification or add additional context in comments.

1 Comment

Yeap, that helped. Indeed simple as that... )) thank you! Marked your answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.