Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass variables from view to controller without using forms in Laravel?

I'm using RESTful controller and passing variables (using forms) works just fine here.

Now for some reason I need to use simple link, created with action() and dedicated route for @create action.

My view creates few similar links with different parameters:

<a href="{!! action('\App\Http\Controllers\Admin\Franch\Category\SubCategoryController@create', array('mainCategoryName' => $mainCategoryName)) !!}">

It works, because I can see this in URL:

/create?mainCategoryName=some_sample_name

But the route doesn't pass variables to the @create action OR controller doesn't recieve it for somereason:

Route::get('admin/franch/sub_categories/create', ['uses' => 'Admin\Franch\Category\SubCategoryController@create');

I wonder how can I pass variables from views to specific controllers, using GET and POST methods?

And this is my @create controller:

public function create($mainCategoryName = false)
{
        dd($mainCategoryName);
....

Which is always gives false.

like image 866
Alexey Mezenin Avatar asked Oct 18 '22 15:10

Alexey Mezenin


1 Answers

Well You can create a function on the link and in that function user Ajax

$.ajax({
  type: "POST",//Or Get
  url: url,
  data: {"var_name":variable},
  success: success,
  dataType: dataType
});

Now you can send you variables in the data. and then you can get value of the variable in your controller by:

Input::get('var_name');
like image 60
Sushant Yadav Avatar answered Oct 22 '22 00:10

Sushant Yadav