用户在laravel注册后注册并登录后如何创建文件夹?

时间:2016-12-30 06:41:39

标签: php laravel

我想在每个用户注册后创建一个单独的文件夹,并在创建文件夹后将其登录。我不知道该怎么做。我尝试了创建文件夹的东西,但重定向到相同的注册页面,说电子邮件已经存在。(即它创建文件夹并在DB中注册用户,但不是登录它尝试再次注册用户)。

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.


}

我知道我应该让用户登录。我不知道如何一起做这两件事。

3 个答案:

答案 0 :(得分: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.


}

答案 2 :(得分: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');

}