How to make download feature (from other website link) in Laravel8?

  laravel, php

Good evening, how to make a download feature (from another website link) in laravel 8?

for example I have a data link "download zip" from the github repository, then I want when I click download (in view) it will download from the github repo link.

actually it can be created in a view like <a href="$data->link">, but this method can’t add the "download" value in the table.

I want to do this in the controller and when there is a download request it will also add value to the download field (table).

web.php

Route::get('source-code/download/{id}', [FrontController::class, 'download'])->name('download');

FrontController.php

public function download($id)
    {
        $sc = Sourcecode::findOrFail($id);
        $sc->increment('download');
        $sc->update();
        
        if ($sc->file)
            {
                $file_path = public_path('storage/'.$sc->file);
        
                return response()->download($file_path);
            }
        
        else
            {
                $headers = [
                    'Content-Type' => 'application/zip',
                 ];
      
                return response()->download($sc->link, 'testing.zip', $headers);
            }         
    }

view

@if($sc->file || $sc->link)
     <a href="{{ route('download', $sc->id) }}" target="_blank"  rel="noopener" class="btn btn-primary btn-sm text-white">
        Download
     </a>
@endif

Currently, I can download from the folder, but from other website links, I still can’t. how to make a feature like this? thank you

Source: Laravel

Leave a Reply