2016-11-13 21 views
0

laravelのデフォルトの登録とログイン形式を使用しています。私は何が起こりたいのですか?ユーザーが正常に登録した後にデータを保存する方法です。私はこれをやってみましたが、Time_trackerテーブルにデータを格納していないので、エラーは発生していません。誰かが私を助けることができますか?Laravel 5.2登録後に別のデータベースにデータを保存する

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

あなたは '.env'ファイル内に' DB'を設定していますか?これはテーブルにデータを挿入する最善の方法ではありません。 –

+0

@RimonKhan yea。私の他のコントローラではDBテーブルに適切に保存されているので、その部分に問題はありません。私の問題はこの部分にあります。 – Ace

答えて

0

あなたはそれ以下のコードが実行されていない理由は、あなたがリターンを削除し、このように最後にそれを置くべきです作成したユーザー戻ってきている。

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 
} 

希望します。

+0

エラーがあります。 AuthController.php行のErrorException 84: 空の値からデフォルトオブジェクトを作成しています。 – Ace

+0

うわー。どうもありがとう! – Ace

+0

それがあなたの問題を解決することを知ってうれしい...! –

関連する問題