2016-12-30 93 views
1

我想爲每個用戶創建一個單獨的文件夾,當他們註冊並在文件夾創建後登錄它們。我不知道該怎麼做。我嘗試了一些創建文件夾但重定向到相同的註冊頁面,說明電子郵件已存在(即創建文件夾並將用戶註冊到數據庫中,但不是登錄而嘗試重新註冊用戶)。如何在用戶註冊並在laravel註冊後登錄他時創建文件夾?

protected function create(array $data) 
{ 
    User::create([ 
     'name' => $data['name'], 
     'email' => $data['email'], 
     'password' => bcrypt($data['password']), 
    ]); 

     return \Storage::makeDirectory($data['name']); 
     //The above creates folder. 


} 

我知道我應該返回用戶登錄。我不知道如何在一起做。

+0

的地方,我已經給出了答案的代碼,讓我知道 – msonowal

回答

1

剛創建的文件夾,然後像它Laravel返回創建用戶實例:

protected function create(array $data) 
{ 
    $user = User::create([ 
     'name' => $data['name'], 
     'email' => $data['email'], 
     'password' => bcrypt($data['password']), 
    ]); 

    \Storage::makeDirectory($data['name']); 

    return $user; 
} 
+1

正是我現在這樣做,它的工作原理,我正要回答我的問題。謝謝 –

0
protected function create(array $data) 
{ 
    $user = User::create([ 
       'name' => $data['name'], 
       'email' => $data['email'], 
       'password' => bcrypt($data['password']), 
      ]); 
    auth('your-guard-name')->login($user); 
    //this logs the user into the application 

    return \Storage::makeDirectory($data['name']); 
    //The above creates folder. 


} 
+0

當我註冊,我得到此錯誤 傳遞給Illuminate \ Auth \ SessionGuard :: login()的參數1必須實現接口Illuminate \ Contracts \ Auth \ Authenticatable,布爾給定,在C:\ xampp \ htdocs \ laravel-projects \ ctohire \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Auth \ RegistersUsers.php在第63行定義。 但它會創建文件夾並在我重新加載錯誤頁面時登錄。 –

0

首先,我不認爲爲每個用戶創建一個新文件夾是一種好的做法。如果您想存儲與用戶有關的東西並識別它們,您可以將file_name映射到user_id或將一些user_id標識附加到file_name。但是,在您的情況下,請嘗試下面的代碼。

protected function create(array $data) 
{ 

//create user and store it in variable called user 
$user = User::create([ 
    'name' => $data['name'], 
    'email' => $data['email'], 
    'password' => bcrypt($data['password']), 
]); 

//make the folder with user_id, since the duplication can happen in name. Here the path is defined and the folder name is created with user id and the folder permission 755 is given 
$makeDir = File::makeDirectory('/path/to/directory/'.$user->id , 0775); 

//Then auth login user 
Auth::login($user); 

//Then return to dashboard or new view. 
return redirect('/dashboard'); 

}