简体   繁体   中英

Getting users' email - Laravel 5 PHP

I'm attempting to store the email address of any user who uploads a file into an "uploader_name" string variable. I've attempted using the following code whichout success - attempting to upload the file will result in a Class 'App\\Http\\Controllers\\Auth' not found error message being thrown back. I know this is a pretty basic question, but I can't seem to find any answers which specifically work with Laravel 5.

This is what I'm trying to use currently without success.

$filelist->uploader_name = Auth::user()->email;

If it helps, here's the full function which saves some of the automatic information of the file.

public function store()
{
    $filelist = new Filelist($this->request->all());
    //store file details - thanks to Jason Day on the unit forum for helping with some of this
    $filelist->name = $this->request->file('asset')->getClientOriginalName();
    $filelist->file_type = $this->request->file('asset')->getClientOriginalExtension();
    $filelist->file_size = filesize($this->request->file('asset'));
    $filelist->uploader_name = Auth::user()->email;
    $filelist->save();
    // Save uploaded file
    if ($this->request->hasFile('asset') && $this->request->file('asset')->isValid()) {
        $destinationPath = public_path() . '/assets/';
        $fileName = $filelist->id . '.' . $this->request->file('asset')->guessClientExtension();
        $this->request->file('asset')->move($destinationPath, $fileName);

    }



    return redirect('filelists');
}

You get not found error, because you don't use the right namespace.

You can resolve this if you use the use keyword like this:

use Auth;

public uploadController {
...
}

Or you can use the full namespace:

$filelist->uploader_name = \Auth::user()->email;

You are getting Class 'App\\Http\\Controllers\\Auth' not found error because you are not using the correct namespace:

There are two ways to resolve this:

By including use keyword in your controller

Example:

use Auth;

UploadController{
     ....
}

or by specifying the full namespace directly like so

$filelist->uploader_name = \Auth::user()->email;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM