'How can a voyager link for downloading the file uploaded by voyager be defined in a cutome view?
I have tried to code a cutome view in which i would be able to code an html tag to directly download the file which was uploaded before with voyager admin panel. here is my route
Route::get('/download/{research}',[App\Http\Controllers\ResearchController::class, 'download'])->name('download');
here is the html tag:
<a href="{{ route('download',$research) }}" target="_blank" class="btn btn-outline-info form-control">Download</a>
help me in the controller bellow
public function download(Research $research)
{
}
Solution 1:[1]
I've worked through this question all night long and found out this helpful.
Then I solved it like this:
Controller
public function home()
{
$researches = Research::all();
foreach ($researches as $research){
$download_links[] = json_decode($research->attachment);
}
$departments = Department::all();
$role_id = \TCG\Voyager\Models\Role::all()->where('name','=','student')->first()->id;
$students = User::all()->where('role_id','=',$role_id);
return view('Research.home', compact('researches','departments','students','download_links'));
}
View
{{ $i=0 }}
@foreach($researches as $research)
<div class="row">
<div class="col-md-10">
<button data-toggle="collapse" data-target="#demo{{ $research->id }}" class="btn border text-start form-control" title="click to read abstract">[ {{ ucwords($research->title) }} ] By: {{ $research->user->name }} @if($research->user->student != null) {{ $research->user->student->last_name }} @else {{ $research->user->employee->last_name }}@endif</button>
</div>
<div class="col">
<a href="{{ Voyager::image($download_links[$i++][0]->download_link) }}" download target="_blank" class="btn btn-outline-info form-control">Download</a>
</div>
</div>
<div id="demo{{ $research->id }}" class="collapse row border">
<div class="col-md-12 ">{!! $research->description !!}</div>
</div>
@endforeach
and now is working properly.
Solution 2:[2]
public function download($id) {
$research= Research::where('id', $id)->firstOrFail();
$pathToFile = storage_path('fileName' . $research->file);
return response()->download($pathToFile);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yahia |
| Solution 2 | Dave |
