The Dead Man
The Dead Man

Reputation: 5566

What is the best way to create file folder for specifi user in laravel?

I have a simple dashboard in which users can register and do some stuff, now I would like to generate a file with username and his id; something like widget/obama2030.js in Laravel when a user is registered

my app structure:

myapp

-app
-bootstrap
-database
-resource
 .....
-widget
 -------

in my user registration controller, I have the following.

protected function create(array $data)
{
    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        ]);
   
    File::makeDirectory('/widget/'.$user->name.Auth::user()->id, 0755, true);

    return $user;
}

Now when user click the button register I am getting the following error

ErrorException (E_NOTICE)
Trying to get property of non-object

what is wrong with my code????

Upvotes: 0

Views: 474

Answers (2)

ceejayoz
ceejayoz

Reputation: 180024

You've created a user, but you haven't logged them in as that newly created user.

As such, Auth::user() is null, and (null)->id won't work.

After creating your user, log them in:

Auth::login($user)

and your code should work.

That said, it's typically not necessary (and it's quite messy) to actually create files for each user - you can almost certainly handle this via a Laravel route, instead.

Upvotes: 2

delboy1978uk
delboy1978uk

Reputation: 12365

Auth::user() isn't returning anything, so it flakes out when you try and access ->id

Upvotes: 0

Related Questions