Laravel PHP-将db中的set选项在选择列表中选择

时间:2018-08-12 21:29:02

标签: php laravel

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

    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都有一个status列,该列代表一个statuses.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

这样做的最佳实践是什么?

1 个答案:

答案 0 :(得分:1)

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

  <select>
     @foreach($statuses as $status)
        <option @if($customer->status == $status->id) selected="selected" @endif>{{$status->title}}</option>
     @endforeach
  </select>