Laravel注册:将两行插入分离的表中



我在laravel中插入行的问题。

理论:我使用简单的Laravel身份验证并有两个表。

  • 用户:ID,名称,密码。

  • user_details:id,userId,电子邮件

注册后,将行插入两个表和userId = id(在用户表中)很有用。

registercontroller.php

<?php
namespace AppHttpControllersAuth;
use AppUser;
use AppHttpControllersController;
use IlluminateSupportFacadesValidator;
use IlluminateFoundationAuthRegistersUsers;
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 IlluminateContractsValidationValidator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            '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)
    {
        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'e_r' => $data['e_r'],
        ]);
        $details = UserDetails::create([
            'firstname' => 'joco',
            'lastname' => 'nagy',
            'email' =>$data['email'],
            'position' => 'cleaner',
            'salary' => '250000',
            'amount_holiday' => '40'
        ]);
        return $user;
    }
}

(我刚刚尝试插入假数据。迁移文件中有默认值。)

模型:

user.php

<?php
namespace App;
use IlluminateNotificationsNotifiable;
use IlluminateFoundationAuthUser as Authenticatable;
class User extends Authenticatable
{
    use Notifiable;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'e_r',
    ];
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    public function user_detail(){
       return $this->hasOne("AppUserDetails");
    }
}

错误:

registercontroller.php行74:班 'App http Controllers Auth userDetails'找不到

我不明白为什么要成为我的模型。

您是否包括模型UserDetails

将其包括在上面:

use AppUser;
use AppUserDetails;

UserDetails更改为AppUserDetails

$details = AppUserDetails::create([
        'firstname' => 'joco',
        'lastname' => 'nagy',
        'email' =>$data['email'],
        'position' => 'cleaner',
        'salary' => '250000',
        'amount_holiday' => '40'
    ]);

您应该使用 use语句。use YourNameSpaceUserDetails;

没有此声明,PHP正在您的当前名称空间中寻找用户通讯类,在您的情况下,AppHttpControllersAuth。这就是为什么你得到

'App http controllers auth userDetails'找不到

最新更新