Laravel 8: How to Display and Download Files

This article is related to my previous article on uploading files with Dropzone. This article will therefore assume you have the migration files, controllers and models discussed in that article. Today we’ll take a look at how to set your website up so that files can be downloaded. Let’s do this!!!

Case Scenario

Let’s assume we have a database table called students with columns id, name, gender, picture_path, created_at and updated_at. The picture_path column holds paths to files uploaded using the store() method. Here is an example of a path stored in the picture_path column:


The path was generated with this line:


For more information, view the previous article. Here are a few steps you can follow to setup downloads on your website:

1. Update your routes

You will need two routes: one for handling request for the page where you will be displaying the files available and the other for handling file download requests. Add the following lines of code to your routes/web.php file.

//Route for viewing a list of student names and links to download their imageRoute::get('view-students', [StudentController::class, 'viewFiles'])->name('student.view_files');//Route for downloading a file using the id of the related studentRoute::get('download/{id}', [StudentController::class, 'downloadFile'])->name('');

2. Create and update your view.

The view will be used to show a list of students names. Beside each student name will be a download link to allow users to download the students picture. To create the view, navigate to your resources/views folder of your project and create a new file with the name view_students.blade.php.

Add the following code to your file:

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>View Students</title></head><body><table>@foreach ($students as $student)<tr><td>{{$student->name}}</td><td><a href="{{ route('', $student->id) }}">download</a></td></tr>@endforeach</table></body></html>

3. Update your Controller

Add the following code to your StudentController:

<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use App\Models\Student;use Illuminate\Support\Facades\Storage;class StudentController extends Controller{  public function create(){   return view('students');  }public function viewFiles(Request $request){  $students = Student::all();  return view('view_students', ['students' => $students]);}public function downloadFile($id){  $path = Student::where("id", $id)->value("file_path");  return Storage::download($path);}}

Don’t forget to add the following line; you need it in order to use Storage::download().

use Illuminate\Support\Facades\Storage;

4. You’re done !!!

You should be able to access a view where you can view student names and download their pictures by running the following in your browser address bar.


Related Articles

How to Upload and Store Files with Laravel 8

Laravel 8: MySQL Database Query Tip Sheet #4




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Data Structures and Algorithms: Complete Review

✨NO.1 software development company in Hosur.

5 Best Gmail Backup Tools in 2020

ARKit + ARCore Tutorial: What did Google bring to the iOS platform?

Some Basic Linux Commands

RADARCTF2019 — Misc(+Programming) Writeup

Build Custom Apps with Power Apps Component Framework

Open Source Software (OSS) Quality Assurance — A Milvus Case Study

Cover image

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kenpachi Zaraki

Kenpachi Zaraki

More from Medium

Laravel | How to extend Notifications and use your custom fields

How to Create login, register, and logout manually in laravel.

Animated display images one after one with VueJS & TailwindCSS

How to Implement Select2 with Alpinejs & Laravel Livewire 2