代码之家  ›  专栏  ›  技术社区  ›  Ben Beri

Laravel PHP-将db中的set选项设置为select列表中的selected

  •  0
  • Ben Beri  · 技术社区  · 7 年前

        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
    

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

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

    1 回复  |  直到 7 年前
        1
  •  1
  •   Simon R    7 年前

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

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