Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RobertF's avatar

Laravel 11 Download File for user

Hello, I am trying to create a ‘client portal’ using Laravel 11 and React.js. Part of the functionality will allow the site administrator to upload specific files for specific users. I have created the code required to upload a document to a specific user. In my documents table, I have added an assigned_user_id field that links that id to a specific user in the users table. However, I need an efficient way to allow a user to click a link to view and download the uploaded document. I have created a react page where the download link will be located. Any help would be appreciated.

I have included a snippet from my Documents Controller:

public function store(StoreDocumentsRequest $request)
{
    // Validate the incoming request
    $data = $request->validated();
    /** @var $file UploadedFile */
    $file = $data['file'] ?? null;
    $data['created_by'] = Auth::id();
    $data['updated_by'] = Auth::id();
    // If a file is uploaded, store it in storage under file
    if ($file) {
        $data['file_path'] = $file->store('file/' . Str::random(), 'local');
    }
    // Create a new file with the validated data
    $create = Documents::create($data);

    if($create) {
        // Flash a success notification and redirect to the document index page
        session()->flash('notify.success', 'Document was uploaded.');
        return redirect()->route('documents.index');
    }

    return abort(500); // Return a server error if the file creation fails
}

Snippet from Resource:

public function toArray(Request $request): array
{
    return [
        'id' => $this->id,
        'name' => $this->name,
        'description' => $this->description,
        'created_at' => (new Carbon($this->created_at))->format('Y-m-d'),
        'file_path' => $this->file_path && !(str_starts_with($this->file_path, 'http')) ?
            Storage::url($this->file_path) : '',
        'project_id' => $this->project_id,
        'project' => new ProjectResource($this->project),
        'assigned_user_id' => $this->assigned_user_id,
        'assignedUser' => $this->assignedUser ? new UserResource($this->assignedUser) : null,
        'createdBy' => new UserResource($this->createdBy),
        'updatedBy' => new UserResource($this->updatedBy),
    ];
}

Snippet from Request:

public function rules(): array
{
    return [
        "name" => ['required', 'max:255'],
        'file' => ['nullable', 'mimetypes:application/pdf'],
        "description" => ['nullable', 'string'],
        'project_id' => ['required', 'exists:projects,id'],
        'assigned_user_id' => ['required', 'exists:users,id'],
    ];
}
0 likes
2 replies
martinbean's avatar

@robertf Just have the link open a URL that will then return a download response:

public function download(Document $document)
{
    // Authorise user can download file...

    return response()->download($pathToFile, $name, $headers);
}

I also don’t get why you make file nullable if you’re intending to create a document from a file, but then allow null values?

RobertF's avatar

@martinbean Thx for the quick response... I am now trying to integrate User authorization with my download response. Its a bit more difficult than I anticipated. Also, I didn't realize my 'nullable' mistake...thx

Please or to participate in this conversation.