我使用默认注册并以laravel的形式登录。我想要发生的是如何在用户成功注册后存储数据。我试过这样做,但它没有在Time_tracker表中存储任何数据,也没有发生任何错误。有人可以帮助我吗?
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();
}
}
答案 0 :(得分:0)
您正在返回创建的用户,这就是为什么下面的代码没有运行的原因,您应该删除return并以这种方式将其放在最后:
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
}
希望这有帮助!