如何使用Laravel 5.2更新My值

时间:2016-11-28 12:09:06

标签: php mysql laravel-5

这是My Controller中的我的addpermission函数,我需要更新函数来更新值。如何在此处添加更新代码。 (我需要在相同的addpermission函数中更新函数)

public function addPermission(Request $request, $id,  Permission $permission)
    {
         $this->validate($request, [
            'status'   => 'required'


        ]);

        $permission = new Permission;

        $permission->status = $request->input('status');
        $permission->project_id       = $id;
        $permission->collaborator_id =  $request->input('cid');
        $permission->save();
        return redirect()->back()->with('info','Permission has been added to your Collaborator successfully');
}

更新 我改变代码如下

public function addPermission(Request $request, $id, $permission = null)//, Permission $permission)
{
  $this->validate($request, [
    'status'   => 'required'
  ]);

  if(is_null($permission)) {
    $permission = new Permission;
    $msg = 'Permission has been added to your Collaborator successfully';
  } else {
    $permission = Permission::findOrFail($permission,['id'])->id;
    $msg = 'Permission updated successfully';
  }

  $permission->status = $request->input('status');
  $permission->project_id       = $id;
  $permission->collaborator_id =  $request->input('cid');
  $permission->save();
  return redirect()->back()->with('info', $msg);

数据插入成功。但是当我点击按钮进行更新时更新不成功将其作为新记录插入到DB中。

我的表名是权限,并且有这些列

id (autoincrement)
status
project_id
collaborator_id

如何解决这个问题

新更新

好的,这是我的表格

form class="form-vertical" role="form" method="post" action="{{ route('projects.collaborators.permission', $project->id) }}">
                        <!--<div id="cid" name="cid">{{ $collaborator->user()->first()->id}}</div>-->
                    <input type="hidden" id="cid" name="cid" value="{{ $collaborator->user()->first()->id }}" />
                    <div class="form-group{{ $errors->has('status') ? ' has-error' : '' }}">
                    <label for="status" class="control-label">Choose Permission</label>
                    <select name="status" id="status">
                    <option value="">Choose a status</option>
                    <option value="3">View Only</option>
                    <option value="2">Edit Tasks</option>
                    <option value="1">Admin</option>
                    </select>
                    @if ($errors->has('status'))
                    <span class="help-block">{{ $errors->first('status') }}</span>
                    @endif
                    </div>
                      <div class="form-group">
                        <button type="submit" class="btn btn-default">Create</button>
                      </div>
                      <input type="hidden" name="_token" value="{{ csrf_token() }}"> 

1 个答案:

答案 0 :(得分:0)

您的控制器可以写成:

public function addPermission(Request $request, $project_id, $permission_id = null)
{
  $this->validate($request, [
    'status'   => 'required'
  ]);

  if(is_null($permission_id)) {
    $permission = new Permission;
    $msg = 'Permission has been added to your Collaborator successfully';
  } else {
    $permission = Permission::find($permission_id);
    $msg = 'Permission updated successfully';
  }

  $permission->status = $request->input('status');
  $permission->project_id       = $id;
  $permission->collaborator_id =  $request->input('cid');
  $permission->save();
  return redirect()->back()->with('info', $msg);
}

所以你的路线可以是:

添加

/project/{project_id}/permission

更新

/project/{project_id}/permission/{permission_id}

<强>更新

假设当表单作为编辑打开时,视图中将有一个$permission变量,那么您可以将表单操作编写为:

<form class="form-vertical" 
            role="form" 
            method="post" 
            action="{{ isset($permission) ? route('projects.collaborators.permission', ['project_id' => project->id, 'permission_id' => $permission->id]) : route('projects.collaborators.permission', ['project_id' => project->id]) }}">

您的路线将如下:

Route::post('projects/{project_id}/permission/{permission_id‌​?}', [ 'uses' => 'ProjectCollaboratorsController@addPermission', 'as' => 'projects.collaborators.permission',]);