Home Ask Login Register

Developers Planet

Your answer is one click away!

tonyweed January 2017

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


{!! 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>

    <!-- {{ $round++ }} -->


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


    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;







Alexey Mezenin January 2017

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.

Post Status

Asked in January 2017
Viewed 1,726 times
Voted 5
Answered 1 times


Leave an answer

Quote of the day: live life