【问题标题】:Laravel 5.1 Authentication - Create user data in another tableLaravel 5.1 身份验证 - 在另一个表中创建用户数据
【发布时间】:2016-01-20 15:40:33
【问题描述】:

我的身份验证和一切正常。我可以登录/注销等。我有两张表,一张称为用户,这是默认的身份验证,第二张称为播放器,将用于播放器数据。我做了一个 Player 模型。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Player extends Model
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'players';

        /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['username', 'wood'];
}

并将 AuthController 类编辑为:

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * 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|min:4|max:32|unique:players',
            'name' => '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([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

        return Player::create([
            'username' => $data['username'],
            'wood' => 0,
        ]);
    }
}

当我注册一个新用户时,用户数据会在 users 表中创建,但我的 player 表没有任何反应。它只是空的。

【问题讨论】:

  • 我将此标记为需要编辑;但是,更具体地说:您应该证明您为诊断问题所做的工作……比`...更具信息性的东西...但是我的玩家表没有任何反应。它只是空的。”将使 SO 社区更容易为您提供帮助。

标签: php laravel authentication laravel-5.1


【解决方案1】:

这是因为您在将数据插入用户表后退出方法。 此处代码错误:

return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

需要:

 protected function create(array $data)
    {
         User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

        return Player::create([
            'username' => $data['username'],
            'wood' => 0,
        ]);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    • 2015-09-23
    • 1970-01-01
    相关资源
    最近更新 更多