duandang2838 2016-05-03 13:14
浏览 56
已采纳

Laravel 5.2扩展脚手架附带的Auth以使用附加表

I am just learning to use Laravel and so far have set up a basic instance and then ran the php artisan make:auth command which setup the basic user registration etc.

What I want to do now is extend this auth to save an additional field from the registration in to a separate related table from Users called Titles.

I have managed to follow the user guide so far. This is where I am at just now:

In register.blade.php I have added in the additional field:

 <div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
                        <label class="col-md-4 control-label">Title</label>

                        <div class="col-md-6">
                            <input type="text" class="form-control" name="title" value="{{ old('title') }}">

                            @if ($errors->has('title'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('title') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

Then in the AuthController.php I have added the title field to the validation:

 protected function validator(array $data)
{
    return Validator::make($data, [
        'title' => 'required|max:100',
        'name' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|min:6|confirmed',
    ]);
}

/**
 * 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']),
    ]);
}

I then created a Title model and related it to the user:

 namespace App;

 use Illuminate\Database\Eloquent\Model;

  class Title extends Model
 {
        public function user()
        {
            return $this->belongsTo('User');
        }

  }

Then I updated the user model:

 class User extends Authenticatable
 {
/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];


public function title()
{
    return $this->hasOne('App\Title');
}

 }

The migration for the Titles table looks like this so I think its correct:

public function up()
{
    Schema::create('titles', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title', 100);
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->timestamps();
    });
}

This is where I am sort of stuck now. How do I actually save the title field to the titles table and have it related to the user?

I see from the documentation that you use a format similar to:

$title = new Title(array('title' => 'test title'));

$title = $user->titles()->save($title);

But I am not sure how to apply it in this situation since the create function in the AuthController just returns a array right away.

protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
    ]);
}
  • 写回答

1条回答 默认 最新

  • dpvr49226 2016-05-03 14:20
    关注

    Firstly, allow mass assignment you can check here Mass Assignment and add fields like title in protected $fillable = ['title'];

    Then use as below:

     protected function create(array $data) { 
         $user = User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); 
         $title = new Title(['title' => $data['title']]); $user->title()->save($title); 
         return $user; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符