我在帐户表的用户表中添加了一个带有外键约束的列account_id
。
对于注册过程,我现在希望首先将email
插入到帐户表中。然后我想用account_id
和所有其他注册信息填充users表。
我的 RegisterController.php 如下所示:
<?php
namespace App\Http\Controllers\Auth;
use App\Account;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* 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, [
// 'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
$account = Account::create([
'email' => $data['email'],
]);
return User::create([
// 'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'account_id' => $accounts['id'],
]);
}
}
我的 Account.php 如下所示:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Account extends Model
{
public function user()
{
return $this->hasOne('User','account_id');
}
}
我的 Users.php 看起来像这样:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function account()
{
return $this->belongsTo('Account');
}
}
这似乎不起作用。我是Laravel的新手,我在这里有点失落。任何指针都会很棒!
答案 0 :(得分:1)
请在顶部添加以下内容
use App\Account;
您需要在控制器上定义模型名称。这就是你错误的原因。
<强>更新强>
在namespace
RegisterController
中使用namespace App\HTTP\Controllers\Auth;
时:
RegisterController.php
然后,您的App\HTTP\Controllers\Auth
文件必须位于php artisan make:controller RegisterController
内。
使用artisan命令创建控制器时,如下所示:
RegisterController.php
然后,默认App\HTTP\Controllers\
在namespace
处创建。因此,您需要将namespace App\HTTP\Controllers;
更改为
using System;
using Abp.Dependency;
using MyProject.EntityFrameworkCore;
using MyProject.Identity;
using Castle.MicroKernel.Registration;
using Castle.Windsor.MsDependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace MyProject.Tests.DependencyInjection
{
public static class ServiceCollectionRegistrar
{
public static void Register(IIocManager iocManager)
{
var services = new ServiceCollection();
IdentityRegistrar.Register(services);
services.AddEntityFrameworkInMemoryDatabase();
var serviceProvider = WindsorRegistrationHelper.CreateServiceProvider(iocManager.IocContainer, services);
var builder = new DbContextOptionsBuilder<MyProjectDbContext>();
builder.UseInMemoryDatabase(Guid.NewGuid().ToString()).UseInternalServiceProvider(serviceProvider);
// This line is for db context registration
iocManager.IocContainer.Register(
Component
.For<DbContextOptions<MyProjectDbContext>>()
.Instance(builder.Options)
.LifestyleSingleton()
);
}
}
}
然后你的问题就会解决。