如何使用Laravel 5.2以相同的方法添加和更新表行

时间:2016-09-09 09:26:50

标签: php mysql laravel-5

我需要使用Laravel 5.2在My Controller中的一个方法中插入数据并更新表格行。我的表名是“权限”。这是我的方法:

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


    ]);

    $permission = new Permission;

    $permission->status = $request->input('status');
    $permission->project_id       = $id;

    $permission->collaborator_id =  $request->input('cid');

    $values = $request->all();
    $permission->fill($values)->save();

    return redirect()->back()->with('info','Permission has been updated to your Collaborator successfully');
    }
    else  {

     $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');

   }  
}

我收到以下错误

  

语法错误,意外的'其他'(T_ELSE)

我做错了什么?

1 个答案:

答案 0 :(得分:1)

对于语法Erorr

if ($permission = Permission::findOrFail($id)); { ......

只需删除";"

if ($permission = Permission::findOrFail($id)) { .....