在Laravel中成功更新用户数据时登录

时间:2018-03-08 12:04:50

标签: laravel

RegisterController.php

我在这里添加了一个更新功能,以便当用户想要使用Facebook登录时,他/她将被重定向到一个表单然后填写这些字段,以便他们的信息将存储在数据库中。

protected function create(array $data)
{
    if ($data['userEmail']) {
        return User::where('email', $data['userEmail'])
            ->update([
                'phone_number' => $data['phone_number'],
                'address' => $data['address'],
                'country' => $data['country'],
                'city' => $data['city'],
                'zip_code' => $data['zip_code'],
                'state' => $data['state'],
                'is_online' => true,
            ]);
    } else {
        return User::create([
            'full_name' => $data['full_name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'phone_number' => $data['phone_number'],
            'address' => $data['address'],
            'country' => $data['country'],
            'city' => $data['city'],
            'zip_code' => $data['zip_code'],
            'state' => $data['state'],
            'is_online' => true,
        ]);
    }
}

IF语句返回true时的错误是

  

“类型错误:参数1传递给   Illuminate \ Auth \ SessionGuard :: login()必须实现接口   Illuminate \ Contracts \ Auth \ Authenticatable,给定整数,调用   C:\ XAMPP \ htdocs中\ esoftwaredeals \供应商\ laravel \框架的\ src \照亮\基金会\验证\ RegistersUsers.php   在第35行“。

但是,如果它返回false,则不会出现错误,并且会创建一个新用户并自动重定向到“/ my-account”页面,这是我想在用户成功更新其信息时重定向的页面。 / p>

3 个答案:

答案 0 :(得分:1)

您需要从User方法返回create()个实例:

protected function create(array $data)
{
    if ($data['userEmail']) {
        $user = User::where('email', $data['userEmail'])->first();
        $user->update([
                'phone_number' => $data['phone_number'],
                'address' => $data['address'],
                'country' => $data['country'],
                'city' => $data['city'],
                'zip_code' => $data['zip_code'],
                'state' => $data['state'],
                'is_online' => true,
            ]);
    } else {
        $user = User::create([
            'full_name' => $data['full_name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'phone_number' => $data['phone_number'],
            'address' => $data['address'],
            'country' => $data['country'],
            'city' => $data['city'],
            'zip_code' => $data['zip_code'],
            'state' => $data['state'],
            'is_online' => true,
        ]);
    }

    return $user;
}

此外,您应该使用updateOrCreate()方法来保持代码的可维护性。例如:

protected function create(array $data)
{
    $data['password'] = bcrypt($data['password']);

    return User::updateOrCreate(
        array_only($data, ['email', 'full_name']),
        array_except($data, ['email', 'full_name'])
    );
}

答案 1 :(得分:0)

更新查询返回受更新查询影响的行数。因此,当更新用户成功时,它将返回1.

create方法返回已保存的模型实例。所以这会导致代码中的问题。

您可以使用eloquent提供的查找和保存方法,并在if语句中返回用户对象,它将起作用。

$user = User::where('email', $email)->first();
$user->firstname = $firstname;
$user->lastname = $lastname;
$user->save();
return $user;

尝试这样,它应该有效。

答案 2 :(得分:-1)

             $user = User::find($usuario->ID);
             $user->rol_id = 1;
             $user->save();
         }
      }
  }


    return view('dashboard.index')->with(compact('cantReferidosDirectos', 'cantReferidosIndirectos', 'cantReferidosActivos', 'fechaProxActivacion', 'new_member',
                'cantventas', 'cantventasmont', 'fullname', 'rangos', 'cantAllUsers', 'rankingComisiones', 'rankingVentas', 'permiso','noticias', 'contTicket', 'moneda',
                'nombreRol'
        ));
}

/**
 * Permite actualizar las informacion de los usuarios 
 * 
 * @access public
 * @return view
 */
public function ActualizarTodo()
{
    $comisiones = new ComisionesController;
    $comisiones->ObtenerUsuarios();
    $todousers = $this->generarArregloUsuario(Auth::user()->ID);
    foreach ($todousers as $user ) {
        if ($user['rol'] != 0) {
            $activacion = new ActivacionController;
            $activacion->activarUsuarios($user['ID']);
        }
    }

参数 “ compact():未定义的变量:fechaProxActivacion” Como soluciono este错误