Tyrx
Tyrx

Reputation: 101

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');
}

Upvotes: 0

Views: 98

Answers (2)

Peter Kota
Peter Kota

Reputation: 8340

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;

Upvotes: 1

Emeka Mbah
Emeka Mbah

Reputation: 17545

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;

Upvotes: 1

Related Questions