Laravel表单更新无法获得正确的值

时间:2016-05-25 05:06:32

标签: php html forms laravel laravel-blade

所以我提出laravel表单进行更新

Index.blade

 <div class="col-sm-12">
  <div class="formrow row">
    <div class="form-group">
      <div class="divlabel col-sm-2">
      <label>Kode Program Studi:</label>
      <span class="required">*</span>
    </div>
    <div class="divinput col-sm-8">
      <select id="id" data-plugin-selectTwo class="form-control populate placeholder" title="kode program studi harus diisi" name='id' required data-plugin-selecTwo>
        <option value="">-PILIH NAMA USER-</option>
        @foreach ($users as $user)
          <option class="form-control" value = '{{$user->id}}'>{{$user->id.' | '.$user->name}}</option>
        @endforeach
        <label class="error" for="id"></label>
    </select>
    </div>
  </div>

  {!! Form::open(['url' => 'master/hakakses/'.$user->id,'method' => 'PATCH','class'=>'update']) !!}
  <!-- {!! Form::model($user,['route'=>['master.hakakses.update', $user->id],'method' => 'PATCH','class'=>'update']) !!} -->


  <div class="form-group">
    <div class="divlabel col-sm-2">
    <label>Kode Program Studi:</label>
    <span class="required">*</span>
  </div>
  <div class="divinput col-sm-8">
    <select id="role" data-plugin-selectTwo class="form-control populate placeholder" title="kode program studi harus diisi" name='role_id' required data-plugin-selecTwo>
      <option value="">-PILIH HAK AKSES-</option>
      @foreach ($roles as $role)
        <option class="form-control" value = '{{$role->id}}'>{{$role->id.' | '.$role->role_akses.' | '.$role->role_name}}</option>
      @endforeach
      <label class="error" for="role"></label>
  </select>
  </div>
</div>
</div>
<div class="col-sm-offset-4 col-sm-50">
    <input type="submit" value="Ubah" name = 'simpan' class = 'btn btn-primary'>
  <td><a href="{{ url('master/hakakses')}}" class="btn btn-primary">Kembali</a></td>
</div>
</div>

  {!! Form::close() !!}

控制器:

public function index()
    {
        $data=new HakAkses;

        $users= $data->ListUser();
        $roles= $data->ListRole();
        return view ('Master.HakAkses.index',compact ('users','roles'));
    }
    public function update(Request $request, $id)
    {

return $id;

    }

型号:

public static function ListUser()
    {
      $table = DB::select( DB::raw("SELECT * FROM users"));
      return $table;
    }
    public static function ListRole()
    {
      $table = DB::select( DB::raw("SELECT * FROM m_role"));
      return $table;
    }

问题是当我尝试返回{{$role->id}}时,我无法获得$id的值,该值是数据库中id的最新输入。我认为问题出在FORM

{!! Form::open(['url' => 'master/hakakses/'.$user->id,'method' => 'PATCH','class'=>'update']) !!}

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我看到问题是你要经过这里:

{!! Form::open(['url' => 'master/hakakses/'.$user->id,'method' => 'PATCH','class'=>'update']) !!}

$user->id,但您希望进入控件{{$role->id}}

所以溶剂化在你的控制器中:

public function update(Request $request, $id)
{
    // $id is a user's id

    $roleId = $request->input('role_id');
}