Home > Net >  laravel Button onclick does not pass value but value is assigned
laravel Button onclick does not pass value but value is assigned

Time:10-21

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

  @foreach($files as $file)
                    <tr>
                        <td>{{$file->title}} </td>
                        <td>
                            <a  style="background: orange;" href="{{ route('partner.file.show', $file->id) }}">View</a>
                        </td>
                        <td>
                            <a  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

CodePudding user response:

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');
  • Related