dongtao4319 2015-10-03 14:03
浏览 150
已采纳

获取用户的电子邮件 - 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');
}
  • 写回答

2条回答 默认 最新

  • duan1989643 2015-10-03 14:24
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?