Laravel PHP - 让db中的set选项成为选择列表中的选中

问题描述 投票:0回答:1

我从数据库中选择了两条记录:

    return view('customers')->with(
        [
            "customers" => DB::table('people')
                ->leftJoin("statuses", "people.status", "=", "statuses.id")
                ->where('type', 'customer')
                ->get()->toArray(),
            "statuses"  => DB::table("statuses")->get()->toArray()
        ]);

每个customer都有一个代表statusstatuses.id列。

现在在我看来:

                            @foreach ($customers as $customer)
                                <tr>
                                    <td>{{$customer->id}}</td>
                                    <td>{{$customer->first_name}} {{$customer->last_name}}</td>
                                    <td>{{$customer->country_id}}</td>
                                    <td>{{$customer->phone_number}}</td>
                                    <td>{{$customer->created_at}}</td>
                                    <td>{{$customer->email}}</td>
                                    <td>{{$customer->campaign}}</td>
                                    <td>
                                        <select>
                                            @foreach($statuses as $status)
                                                <option>{{$status->title}}</option>
                                            @endforeach
                                        </select>
                                    </td>
                                    <td>
                                    </td>
                                </tr>
                            @endforeach

我打印所有状态,但我想将所选状态设置为我与people加入的ID

这样做的最佳做法是什么?

php laravel
1个回答
1
投票

您需要根据状态ID检查客户状态

  <select>
     @foreach($statuses as $status)
        <option @if($customer->status == $status->id) selected="selected" @endif>{{$status->title}}</option>
     @endforeach
  </select>
© www.soinside.com 2019 - 2024. All rights reserved.