0

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????

7
  • 2
    1) What line does that refer to? 2) Why even do this? Creating per-user files is typically a bad idea. You want to make an endpoint using PHP will will dynamically serve the expected data, it doesn't need to physically exist as a file. Commented Feb 14, 2020 at 13:44
  • @deceze I need this file to insert it to user table as URL, later on, this file will contain some user configurations Commented Feb 14, 2020 at 13:54
  • 1
    Yeah… again, it doesn't need to be a physical file, now does it? Commented Feb 14, 2020 at 13:57
  • 1
    You want that when a browser requests the URL /widget/...js to output something that's specific to that user, right?! Well, how do you implement something like a user profile page, or any other regular Laravel page that outputs something specific to the currently logged in user, or otherwise something user-specific? You don't create a bunch of separate HTML files, right? You have a Laravel controller and view that you dynamically generate from info from the database, or whatever. Same thing. Commented Feb 14, 2020 at 14:02
  • 1
    It. Still. Doesn't. Need. To. Be. A. Physical. File. Think in terms of HTTP and server requests to URLs and responses to those. None of the URLs on a server need to exist as physical files. The most sensible thing would probably be to have one actual .js file which every user puts into their website, and that then makes an AJAX call to your server to fetch some user specific information. Commented Feb 14, 2020 at 14:13

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks but now when I check inside folder widget is empty?
@user9964622 What else did you expect File::makeDirectory to do? It makes a directory. That's it.
I mean the code above does not create a directory at all and there is no errors
@user9964622 You probably created it at the server's root because you set the path to '/widget/'.
so if I want the folder to apear the same as app or database folder apears how do I do that?
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.