Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

laravel Button onclick does not pass value but value is assigned

So i created a new function in my controller called ‘download’. I also put the route in the web.php file. but it does not pass any value onclick.

Web.php

Route::group(['middleware' => 'role:partner', 'prefix' => 'partner', 'as' => 'partner.'], function(){

    Route::resource('/dashboard', App\Http\Controllers\partner\PartnerController::class);
   Route::resource('/profile', App\Http\Controllers\partner\ProfileController::class);
   Route::resource('/file', App\Http\Controllers\partner\FileController::class);
   Route::get('/download',[FileController::class, 'download'])->name('file.download');
});

index.blade.php

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  @foreach($files as $file)
                    <tr>
                        <td>{{$file->title}} </td>
                        <td>
                            <a class="btn btn-big btn-succes" style="background: orange;" href="{{ route('partner.file.show', $file->id) }}">View</a>
                        </td>
                        <td>
                            <a class="btn btn-big btn-succes" style="background: green;" href="{{ route('partner.file.download', $file->id) }}" >Download</a>
                        </td>
                        <td>{{@$file->language->name}} </td>
                        @foreach($file->tag as $tags)
                        <td style="background:{{$tags['color']}} ;">{{@$tags->name}} </td>
                        @endforeach
                        
                    </tr>
                    @endforeach

controller:

 public function show($id)
    {
        $file = File::findOrFail($id);

        return view('partner.file.viewfile', compact('file'));
    }

 public function download(Request $request)
    {
    $id = $request->route('id');
        $downloadfile = File::find($id);
        dd($downloadfile);
        try {
        //    return response()->download();
        } catch (\Exception $e) {
            return back();
        }

        return back();
  
    }

the Show route does work but the download does not work

>Solution :

You can get the file id parameter from the route parameters, like this:

public function download(Request $request, int $fileId)
    {
        $downloadfile = File::find($fileId);

        dd($downloadfile);
        try {
        //    return response()->download();
        } catch (\Exception $e) {
            return back();
        }

        return back();
  
    }

And in your web.php you have to update your ‘/download’ get method to add the route parameter.

   Route::get('/download/{fileId}',[FileController::class, 'download'])->name('file.download');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading