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

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


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


It would be much better if you make all your "calculations" in the controller and pass a resulting value to the view like so

class SomeController() extends Controller{    public function getView():View    {        $mainCategoryName = "fix";        $formUrl = action('\App\Http\Controllers\Admin\Franch\Category\SubCategoryController@create', array('mainCategoryName' => $mainCategoryName));        return view('view.show', compact('formUrl'));    }...

Your mistake is in keeping meaningful data in the view, when the view should only have processed values:

<a href="{!! $formUrl !!}">

And if you really want to go "nuts", you can create a class that would generate HTML using a blade-view and the controller data and then you can execute this class in the controller to have your "partial" ready to be incorporated in a view as HTML. But not the other way round. Keep the calculations out of your views.