0

I have a problem downloading a pdf file in laravel with voyager. Someone said, i have to use 'json_decode' but I cant understand how to do so. Here is my controller:

Controller

class HomeController extends Controller
{
    public function index()
    {
        $posts = Post::all();
        $files = Download::all();

        return view('user.index', compact('posts', 'files'));
    }

    public function show($id)
    {
        $files = Download::where('id', $id)->firstOrFail();
        $path = storage_path($files->file);

        return response()->download($path);
    }
}

Route

Route::get('/download/{id}','HomeController@show')->name(downloadFile);

View

@foreach($files as $file)
    <a class="primary-btn" href="{{route('downloadFile',$file->id)}}">Download Resume </a>
@endforeach

The file "C:\laragon\www\kbtonmoy.com\storage[{"download_link":"downloads\January2020\tmUxP8s9Tvzzqwnl7nxT.pdf","original_name":"ridita.pdf"}]" does not exist

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
KB Tonmoy
  • 37
  • 8

1 Answers1

0

At first check your $path I think you shouldn't use storage_path() function. Because Voyager store file info in json format. I guess you should try this.

Controller

class HomeController extends Controller
{
    public function index()
    {
        $posts = Post::all();
        $files = Download::all();

        return view('user.index', compact('posts', 'files'));
    }

    public function show($id)
    {
        $files = Download::where('id', $id)->firstOrFail();

        //$path = storage_path($files->file);

        $new_file = json_decode($files->file)[0];
        $new_file_path = Voyager::image($new_file->download_link);

        $new_file_name = $new_file->original_name; //if you need original file name
        $new_file_size = round((Storage::disk(config('voyager.storage.disk'))->size($new_file->download_link)) / (1024*1024) ,4) ; //if you need file size in MB

        return response($new_file_path);
    }
}

View

@foreach($files as $file)
    <a class="primary-btn" href="{{route('downloadFile',$file->id)}}" download>Download Resume </a>
@endforeach

But I think you should normalize the code by using relationship in Download Model

So then you do not need to route on show function

Then it may look like this

Controller

class HomeController extends Controller
{
    public function index()
    {
        $posts = Post::all();
        $files = Download::all();

        return view('user.index', compact('posts', 'files'));
    }
}

View

<?php 
foreach($files as $file){
    $new_file = json_decode($file->file)[0];
    $new_file_path = Voyager::image($new_file->download_link);

    $new_file_name = $new_file->original_name; //if you need original file name
    $new_file_size = round((Storage::disk(config('voyager.storage.disk'))->size($new_file->download_link)) / (1024*1024) ,4) ; //if you need file size in MB
?>
    <a class="primary-btn" href="{{$new_file_path}}" download>Download Resume </a>
<?php
}
?>

Hope it help you.