注册后无法插入另一个表

时间:2017-11-02 17:25:05

标签: php laravel laravel-5.5

我想在注册后插入userbio这里是我的注册RegisterController

   protected function create(array $data)
    {
        $databio = new ModelBio();
        $maxbio =  $databio->getMaxbioId();
       // die(  $maxbio );

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

        return Biodata::create([
            'biodataId' =>   $maxbio,
            'fullname'  => $data['fullname']
        ]);

    }

它为Users创建新记录。但是,有一个问题我无法插入usersbio,我找不到任何错误消息。顺便说一句RegisterController来自laravel auth。这是我的Biodata

class Biodata extends Model 
{
    //

    protected $primaryKey = 'biodataId';
    protected $table = "usersbio";
    public $incrementing = false;

   protected $fillable = [
     'biodataId','address','fullname','remarks'
    ];

    static function getMaxbioId(){

         $max = Biodata::max('biodataId');
        if($max == ""){
            return "BIO-0001";
        }else{

            $number = substr($max, 4);
            return 'BIO-' . sprintf('%04d', intval($number) + 1);

        }
    }

}

我该如何解决?提前谢谢

1 个答案:

答案 0 :(得分:1)

创建用户时不要写return。它实际上从那里返回,并且不会执行该函数的其他代码。

protected function create(array $data)
{
    $databio = new ModelBio();
    $maxbio =  $databio->getMaxbioId();
   // die(  $maxbio );

    User::create([            // Removed `return` from here
        'username'  => $data['username'],
        'email'     => $data['email'],
        'biodataId' =>   $maxbio,
        'password'  => bcrypt($data['password']),
    ]);

    return Biodata::create([
        'biodataId' =>   $maxbio,
        'fullname'  => $data['fullname']
    ]);

}
相关问题