dtn55928 2017-02-28 14: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 18: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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP