Laravel返回上传的文件而不是路径

时间:2017-02-10 15:25:24

标签: laravel laravel-5

我正在上传文件并将其保存在本地存储中。这些文件存储在与特定用户相关的数据库中。当我返回上次保存的用户时。我正确地得到了其他细节。而不是文件路径。我把文件作为回复

 $file = $request->file('resume');
 $profile_id = $request['profile_id'];
 $original_name = $_FILES['resume']['name'];                
 $resume_new_name = str_random(). '.' . $file->getClientOriginalExtension();
 $new_file = $request->file('resume')->move('resources/assets/resumes',$resume_new_name);
 $candidate_info['resume'] = $new_file;
 $candidate = new candidate();
    if (empty($candidate_info['name'])) {
           $candidate_info['name'] = $candidate_info['email'];
     }
        $candidate->name = $candidate_info['name'];
        $candidate->phone = $candidate_info['phone'];
        $candidate->email = $candidate_info['email'];
        $candidate->resume = $candidate_info['resume'];
        $candidate->client_id = \Auth::user()->id;
        $candidate->status = "new";
        $candidate->profile_id = $profile_id;
        $candidate->save();
        return response()->json(['msg' => $candidate]);

在数据库列'resume'中,它存储路径。但在json响应中它发送整个文件而不是路径。我如何发送路径而不是发送整个文件

2 个答案:

答案 0 :(得分:0)

只需返回该列的值:

return response()->json(['msg' => $candidate->resume]);

答案 1 :(得分:0)

不使用移动,而是使用商店

$new_file = $request->file('resume')->store('resources/assets/resumes',$resume_new_name);