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'],
];
}