James Wilson
James Wilson

Reputation: 13

Laravel passing a variable to a method from a view

I have a view called settings.blade.php

I want to have a button which when clicked calls a function in my HomeController.

settings.blade.php:

<a href="{{action('HomeController@download', Auth::user()->id)}}"><button type="button" class="btn btn-light">Download my user data</button></a>

The idea is it grabs the user_id and sends it in the function to HomeController@download

When I run this, I get this error

Action App\Http\Controllers\HomeController@download not defined. (View: C:\inetpub\www\laravel\baconator\resources\views\settings.blade.php)

However it's defined

public function download($id){
    return $id;
}

At the moment it just returns the ID as I'm testing.

What am I doing wrong?

Upvotes: 0

Views: 61

Answers (2)

Vijay
Vijay

Reputation: 96

For eg:- In your route file add a route

Route::get('/example/{user_id?}', 'HomeController@download')->name('download');

In your view file change html to

<button onclick="window.location.href='{{url('/example').'/'.Auth::user()->id}}'" type="button" ></button>

Upvotes: 0

Exterminator
Exterminator

Reputation: 1246

Define you route like this

Route::get('user/{user}', 'UserController@download')->name('downloadUser');

and define href like this

{{ route('downloadUser',[Auth::user()->id]) }}

Upvotes: 1

Related Questions