如何将数组传递到Laravel控制器

时间:2017-01-11 17:50:52

标签: php arrays laravel

我在Laravel视图中有许多数组,我想传递给控制器​​。这些数组不应由用户修改,只是因为它们是从控制器中的另一个方法传递而来的(视图在控制器方法之间起作用)。我不确定实现这一目标的最佳做法是什么。

TLDR:尝试在$info_array

中将process_logs从View传递给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');

1 个答案:

答案 0 :(得分:2)

如果$info_array只是一个数组,您可以序列化它并传入隐藏的输入:

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

在控制器中反序列化它:

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

如果您需要更高的安全性,可以使用encrypt() and decrypt()帮助程序。