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!

    点赞 打赏 评论

相关推荐 更多相似问题