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

DoeJohn's avatar

Starting points: Upload, list and download files with Laravel

Hi, First of all - my English is bad, sorry for that. I hope that you will understand me.

I want to create a simple application that will have a simple form with upload field. This form will create a Report with attached .pdf file. For example, the form for creating reports will have the following fields:

  • Name (text)
  • Date
  • File (.pdf document, upload field)
  • Submit button

When some user submits this form - report will be created (stored in the database) and file will be saved in public/uploads directory.

After that, user will be redirected to a page that will display all reports in the html table. That table will have three columns: Name | Date | Filename

  • Name (that was entered on the form and saved in DB) will be a link for editing report;
  • Date
  • Filename will be a **link for download **

So, we will have a table reports in the database, a model Report.php and controller ReportsController.php.

Please correct me if I'm wrong:

1. I guess I will need a table in the database with information about the file? If so, lets say that the name of that table is files. Then I will also have a model File.php, but I will not need a controller FilesController.php, right?

2. What fields/attributes table files should have? Since one report can have only one file, I guess that we will have a foreign key report_id? (report has one file, one file belongs to a report) So, I suppose that table files will have the following attributes: id | report_id | filename

3. ReportsController.php@store will be where I save the Report in the database & store uploaded file in the public/uploads directory? I'm not sure how the method store should look like. I think I would do something like this:

public function store(Requests\ReportRequest $request)
{
    $report = $this->reports->create($request->only('name', 'date'));

    $file = new File([
        'report_id' => $report->id,
        'filename' => $request->get('filename')
    ])->save();

    $request->file('filename')->move(base_path() . '/public/uploads/', file('filename'));
    
    return redirect('reports')->with([
            'flash_message' => 'Your report has been created'
        ]);
}

I have a feeling that this is not done properly, that there is a much better way, right?

4. Last question: How to form links to download files? For example, if I have a VIEW index.blade.php - I suppose that ReportsController.php@index is the place to form the links for files (each report has its own file), and pass these links together with all $reports to index.blade.php, right? BUT how would you do that?

0 likes
1 reply
mightylal's avatar

This is up to you, but you could have the file information in the reports table. So you would just add a few more fields to your reports table. I think filepath, filename, and filemimetype would be all you need.

If your html table that lists off all the reports is on the index view then the link to download a file could be something along the lines of /report/{id} and that would be a get request. Then you would have a method similar to this:

public function download($id)
{
    $report = $this->report->find($id);
    return response()->download($report->filepath, $report->name, ['Content-Type:' . $report->filemimetype]);
}
1 like

Please or to participate in this conversation.