0

I was trying to send an array through a route to another view, but when i used the function get_defined_vars(), i realized that i was sending a string with the information. Is it possible to do that?

this form from my view should send the array to my route

            <form action="/trans" method="POST">
                @csrf
                <div class="input-group">
                  <input type="hidden" class="form-control" name="r" value="{{$cooperado}}">
                    <button type="submit" class="btn btn-primary">
                      <span>+</span>
                    </button>
                  </span>
                </div>
            </form>

then this route should send the array to the other view

Route::post('/trans',  function(){
    $j = Input::get('r');
    return view('movs.create')->with(['j'=>$j]);
});

this is the controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Movimentacoes;
class MovimentacoesController extends Controller
{
public function create()
    {
        //
        return view('movs.create');
    }
}
3
  • Your code is not passing in the controller at all?? Commented Jun 13, 2019 at 19:33
  • no, it should right? i'm still learning about MVC Commented Jun 13, 2019 at 19:37
  • if you call return dd($j) what it looks like? may be your $cooperado is formatted as string Commented Jun 14, 2019 at 4:39

2 Answers 2

2

routes.php

Route::post('/trans',  'MovimentacoesController@create');

controller

use Illuminate\Http\Request;
use App\Movimentacoes;

class MovimentacoesController extends Controller
{
    public function create(Request $request)
    {
        $j = $request->request->get('r');
        return view('movs.create')->with(['j' => $j]);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

still getting string, dont know why
<input type="hidden" class="form-control" name="r[]" value="{{$cooperado}}"> attribute r - add square brackets?
now it recognize as an array, but with just one value, the string
you may use "implode" function for convert $cooperado to string, send it via form, and use "explode" function for convert string to array where necessary
1

Code like this In the form tag:

<input type="hidden" class="form-control" name="r[]" value="{{$cooperado}}">
<input type="hidden" class="form-control" name="r[]" value="{{$cooperado}}">
<input type="hidden" class="form-control" name="r[]" value="{{$cooperado}}">

submit this form
then the Input::get('r') will be Array!
I hope it helps you.

Comments

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.