使用Laravel中的下拉列表更改值

时间:2017-12-27 17:52:36

标签: laravel

我想要你的enter image description here建议如果我更改下拉选择选项,那么我想更改我的表值,以便更好地了解我正在上传图片。谢谢

我正在上传我的控制器代码

 public function index()
{
    $allRoom =AllocateClassroom::with('course','department')->paginate(10);

    //dd($allRoom);
    return view('Admin.allocateClassrooms.index',['allRoom'=>$allRoom]);

}

这是我的观看代码

 {{Form::label('department','Department')}}
            <select name="department" id="department" class="form-control">
                <option value=" ">----Select Department-----</option>
                @foreach($allRoom as $value)
                    <option value="{{$value->id}}">{{$value->department->name}}</option>
                @endforeach
            </select>

                    <br>
                        <div class="table-responsive">
                          <table class="table table-bordered">
                                  <tr class="success">
                                      <th>ID</th>
                                      <th>Course Code</th>
                                      <th>Course Name</th>
                                      <th>Schedule Info</th>
                                  </tr>

                                  <tr class="info" id="info">
                                      <td class="success" >{{$value->id}}</td>
                                      <td class="success" >{{$value->course->code}}</td>
                                      <td class="success" >{{$value->course->name}}</td>
                                      <td class="success" >{{$value->   Room_No}}
                                          {{$value->    date}}
                                          {{$value->    start_time}}
                                          {{$value->    end_time}}


                                      </td>
                                  </tr>
                          </table>
                        </div>

2 个答案:

答案 0 :(得分:1)

这就是我这样做的方式:

首先,我添加一个包装select的表单,这样当用户更改其值时,页面会重新加载选择的部门

<form method="get" id="department-form">
    <select name="department" id="department" class="form-control" onchange="document.getElementById('department-form').submit()">
        <option value=" ">----Select Department-----</option>
        @foreach($allRoom as $value)
            <option value="{{$value->id}}" @if($value->id == $department) selected @endif>{{$value->department->name}}</option>
        @endforeach
    </select>
</form>

在我的控制器中我会有这个:

public function index( Request $request)
{
    $department = null;
    if($request->department) $department = $request->department;
    $allRoom =AllocateClassroom::with('course','department')->whereHas('department', function($query) use($department){
        if($department) $query->where(id, $department);
    })->paginate(10);

    return view('Admin.allocateClassrooms.index',['allRoom'=>$allRoom, 'department' => $department]);

}

在这里,我从请求中获取我的部门并用它查询数据

<强>更新

抱歉,最后一段代码是让所有房间和符合条件的部门。现在它只获得符合部门条件的房间

答案 1 :(得分:0)

您正在尝试获取非对象的属性。在提供错误的文件中将{{$value->id}}更改为{{$value['id']}}