一些领域不节约 - Laravel 5.1注册

时间:2015-10-15 00:22:15

标签: laravel-5

在Laravel 5.1中创建新用户时遇到问题

user.php的

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract,
                                AuthorizableContract,
                                CanResetPasswordContract
{
  use Authenticatable, Authorizable, CanResetPassword;

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'laravel_users';

public $timestamps = false;


protected $primaryKey = 'id';


/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
                    'username',
                    'password',
                    'role_id',
                    'email',
                    'avatar',
                    'isActive',
                ];

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = ['password', 'remember_token'];

public function isAdministrator()
{
    return false;
}

public function blogs()
{
    return $this->hasMany('App\Models\Blog');
}
}

authcontroller.php

<?php

namespace App\Http\Controllers\Auth;

use Socialite;
use Illuminate\Routing\Controller;

#use App\Http\Controllers\Controller;

use Redirect;

use App\User;
use Validator;
use Auth;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;

protected $redirectTo = '/admin';

public function __construct()
{
    $this->middleware('guest', ['except' => 'getLogout']);
}
//temporarily redirected to duterte-run.mendanielle.com
public function getRegister()
{
    return view('auth.register');
}
/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        'username' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|confirmed|min:6',
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
protected function create(array $data)
{
    return User::create([
        'username' => $data['username'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}

/**
 * Redirect the user to the GitHub authentication page.
 *
 * @return Response
 */

public function redirectToProvider()
{

    return Socialite::with('facebook')->redirect();
}

/**
 * Obtain the user information from GitHub.
 *
 * @return Response
 */
public function handleProviderCallback()
{

    //retrieve user's information from facebook
    $user  = Socialite::with('facebook')->user();
}
}

我不知道为什么电子邮件字段没有保存,而且php artisan tinker没有返回错误

另外,mysql字段是正确的,并在mysql命令中手动添加值。这可能有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我发现您的电子邮件和电子邮件没有任何问题。密码。您在email中有$fillable属性,并且已将其添加到create方法中,因此如果仍存在问题,那么您可能会将表单字段命名为错误。

如果您正在谈论Socialite Github用户,那么您的handleProviderCallback方法远未完成。 Socialite会为您提供一些用户数据,但随后您可以将该数据转换为应用程序的用户Eloquent模型,并将该用户记录下来。

public function handleProviderCallback()
{
    $fb_user = Socialite::with('facebook')->user();

    // Check if the email is already registered
    $user = User::where('email', '=', $fb_user->getEmail())->first();

    // If not, create a new user
    if (! $user->exists) {
        // Grab data from Facebook
        $data = [
            'username' => $fb_user->getNickname(),
            'email'    => $fb_user->getEmail(),
            'password' => str_random(40),
        ];

        // Use it to create an Eloquent user object
        $user = $this->create($data);
    }

    // Log that user in
    \Auth::login($user);

    // Direct them to the authenticated users page
    return redirect('home');
}

这仍然只是一个shell,您必须处理已经使用电子邮件/用户名等的情况。