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

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();

    }
}

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!