A
A
adam_carraway2018-09-04 16:42:34
Laravel
adam_carraway, 2018-09-04 16:42:34

How to accept variable in laravel controller?

How to pass a variable to the controller via js in laravel and accept it there?
In Codeigniter I did this:

function add(obj) {
        var id = obj.id;
        $.ajax({
            url: "<?php echo base_url(); ?>" + "index.php/main/add_to_card",
            type: "POST",
            data: {"id": id},
            cache: false,
            success: function (response) {
                window.location.reload();
            }
        });
    }

In controller I accept like this: How is it done in laravel?
$id= trim($_POST['id']);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
VitaliyBorys, 2018-09-04
@adam_carraway

function add(obj) {
        var id = obj.id;
        $.ajax({
            url:  document.location.origin+ "/main/add_to_card",
            type: "POST",
            data: {"id": id},
            cache: false,
            success: function (response) {
                window.location.reload();
            }
        });
    }

And in the controller
$id = request()->input('id')

K
Kirill Nesmeyanov, 2018-09-04
@SerafimArts

Replace the url from the example with A. Further "how", "what" and "where" depend on your tasks and needs, and are located on the pages of the documentation you hate so much with blood and love: https://laravel.com/docs/5.7 /blade

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question