Home > Back-end >  How can a voyager link for downloading the file uploaded by voyager be defined in a cutome view?
How can a voyager link for downloading the file uploaded by voyager be defined in a cutome view?

Time:03-13

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" >Download</a>

help me in the controller bellow

public function download(Research $research)
{

}

CodePudding user response:

  public function download($id)
    {
        $research= Research::where('id', $id)->firstOrFail();
        $pathToFile = storage_path('fileName'  . $research->file);
        return response()->download($pathToFile);
    }

CodePudding user response:

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 >
                        <div >
                            <button data-toggle="collapse" data-target="#demo{{ $research->id }}"  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 >
                            <a href="{{ Voyager::image($download_links[$i  ][0]->download_link) }}" download target="_blank" >Download</a>
                        </div>
                    </div>
                    <div id="demo{{ $research->id }}" >
                        <div >{!! $research->description !!}</div>
                    </div>

                @endforeach

and now is working properly.

  • Related