noot
noot

Reputation: 103

How can I pass an Array into a Laravel Controller

I have a number of arrays within my Laravel view that I would like to pass into the controller. These arrays should not be modified by the user, and are only there because they are passed from another method in the controller, (the view is acting as abridge between the controller methods). I'm not sure what the best practice to accomplish this is.

TLDR: Trying to pass $info_array from View to process_logs method in GameController

View

{!! Form::open(['url' => 'admin/games/process']) !!}

@foreach ($players_array as $game)
    <h4>Round {{$round}}</h4>

    <div class="form-group">

        @foreach($game as $player)
            <p><strong>{{ $player }}</strong></p>

            <!-- This chunk might be able to be written cleaner using laravelcollective/html -->
            <select name="round_{{$round}}[{{str_replace("]", "fstupidkey", $player)}}]">
                @foreach($all_players as $key=>$p)
                    <option value="{{ $key }}">{{ $p }}</option>
                @endforeach
            </select>

        @endforeach
    </div>
    <!-- {{ $round++ }} -->

@endforeach

<div class="form-group">
    {!! Form::submit('Submit Refactored Players', ['class' => 'btn btn-outline-primary form-control']) !!}
</div>
{!! Form::close() !!}

Controller

  public function process_logs(Request $request){
        /*
         * Accept player match arrays from user form
         * Input event data into database
         */


    $a = $request->round_1;

    /*
    * Create new array to put shit in..
    */
    $b = [];

    foreach($a as $key=>$item){
        $c = str_replace("fstupidkey", "]", $key);
        $b[$c] = $item;
    }

    dd(Session::get('info_array'));

    dd($request->all());

}

Routes

Route::post('admin/games/process', 'GameController@process_logs');

Upvotes: 2

Views: 5796

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

If $info_array is just an array, you can serialize it and pass in hidden input:

{!! Form::hidden('info', json_encode($info_array)) !!}

And unserialize it in the controller:

$info_array = json_decode($request->info, true);

If you need more security, you can use encrypt() and decrypt() helpers.

Upvotes: 2

Related Questions