如何从laravel中的多选返回选定值的列表?

我有一个多选下拉菜单,它从数据库中获取记录作为选项,所以我需要获取所选记录的 id 列表


<div class="field">

    <label>Mailer</label>

    <div class="ui fluid multiple search selection dropdown">

        <i class="dropdown icon"></i>

        <div class="default text">Select Mailer</div>

        <div class="menu">


            @php

                use App\Mailers;

                $mailers=Mailers::all();

            @endphp


            @foreach ($mailers as $mailer)

                <div class="item">{{ $mailer->fname }}</div>

            @endforeach

        </div>

    </div>

</div>


紫衣仙女
浏览 112回答 1
1回答

慕娘9325324

在 name 属性中使用数组来保存所选选项<div class="menu">&nbsp; &nbsp; @php&nbsp; &nbsp; &nbsp; &nbsp; use App\Mailers;&nbsp; &nbsp; &nbsp; &nbsp; $mailers = Mailers::all();&nbsp; &nbsp; @endphp&nbsp; &nbsp; <select multiple name="mailId[]">&nbsp; &nbsp; &nbsp; &nbsp; @foreach ($mailers as $mailer)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <option value="{{ $mailer->id }}">{{ $mailer->fname }}</option>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; @endforeach&nbsp; &nbsp; </select></div>当您使用表单时,您将在请求对象中的控制器中获取表单值。所以在控制器中foreach ($request->mailId as mail_id) {&nbsp; &nbsp; //here what you need to do with the ids.}在刀片/视图中使用 php 是一种不好的做法。而是从控制器加载变量。
打开App,查看更多内容
随时随地看视频慕课网APP