dtn55928 2017-02-28 06:08
浏览 66
已采纳

Laravel - 在动态表的每一行内附加行id以形成name属性

I send two variable to my view $abilities, and $users. $abilities is an array of key-value pairs containing data in the following format:

["1"=>"create_user","2"=>"delete_user","3"=>"ban_user"]

$users is an array in the following format:

["1"=>"John Doe","2"=>"Jane Doe"]

Both are taken from their respective databases abilities (columns: ID, name) and users (columns: ID, name).

In the view, I have a table which contains a multiple select for each row against the $user->id:

@foreach($users as $user)
<tr>
    <td>{{$user->id}}</td>
    <td>
       {{Form::select("takenabilities[]",$abilities, null, ['id'=>'system-abilities','multiple','class'=>'form-control' ])}}
   </td>
</tr>
@endforeach

Problem is that when I receive the request in my controller, I only see one takenabilities[] array, and I want to save data from each multiple select in each row generated dynamically, if that makes sense?

  • 写回答

1条回答 默认 最新

  • douji1877 2017-02-28 10:27
    关注

    You can't do that using multiple selects with the same name. The value of the last one overrides all previous selects' values. In addition you'd better assign different id to every select. Try to add another level to the array:

    @foreach($users as $user)
    <tr>
        <td>{{$user->id}}</td>
        <td>
           {{Form::select("takenabilities[$user->id][]", $abilities, null, ['id'=>'system-abilities-' . $user->id,'multiple','class'=>'form-control' ])}}
       </td>
    </tr>
    @endforeach
    

    And in controller:

    $takenabilities = [];
    foreach(Request::input('takenabilities') as $userId => $userAbilities) {
        $takenabilities = array_merge($takenabilities, $userAbilities);
    }
    $takenabilities = array_unique($takenabilities);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部