dsw1608 2016-11-13 11:37
浏览 66
已采纳

Laravel 5.2注册后将数据存储在另一个数据库中

Im using the default registration and log in form of laravel. What i want to happen is how do i store a data after the user successfully register. I tried doing this but its not storing any data in the Time_tracker table and there is no any error occured. Can some one help me?

AuthController.php

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Mail\Mailer;
use Illuminate\Http\Request;
use App\Time_tracker;


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;

/**
 * Where to redirect users after login / registration.
 *
 * @var string
 */
protected $redirectTo = 'maintenance';

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

/**
 * 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|max:255',
        'company' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'telephone' => 'required|max:255',
        'password' => 'required|min:6|confirmed',
        'g-recaptcha-response' => 'required|captcha',
    ]);

}

/**
 * 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'],
        'company' => $data['company'],
        'email' => $data['email'],
        'telephone' => $data['telephone'],
        'password' => bcrypt($data['password']),
    ]);

    $user = new Time_tracker;

    $user->current_time = 0;
    $user->purchase_time = 0;
    $user->status = 'not_paid';
    $user->user_id = $id;

    $user->save();

    }
}
  • 写回答

1条回答 默认 最新

  • douluo1330 2016-11-13 11:40
    关注

    You are returning the created user that's why the code below it isn't running, you should remove return and place it at the end in this way:

    protected function create(array $data)
    {
        $user = User::create([   // <------ Removed 'return' from the front of this line
            'name' => $data['name'],
            'company' => $data['company'],
            'email' => $data['email'],
            'telephone' => $data['telephone'],
            'password' => bcrypt($data['password']),
        ]);
    
        $time_tracker = new Time_tracker();
    
        $time_tracker->current_time = 0;
        $time_tracker->purchase_time = 0;
        $time_tracker->status = 'not_paid';
        $time_tracker->user_id = $user->id; // <----- This would be '$user->id' instead of '$id'
    
        $time_tracker->save();
    
        return $user; // <----- 'return' added at the end of the method
    }
    

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型