S
S
snake22021-07-19 11:52:28
PHP
snake2, 2021-07-19 11:52:28

how to pass url to javascript?

There is a route

Route::get('/materials/{material_id}/link/{link_id}', [MaterialController::class, 'showLink']);

When navigating through it, the showLink method is called
public function showLink()
    {
       return response()->json([
           "link" => Link::find(15)
       ]);
    }


Ajax request
$('.edit-link').click(function(){
    var date = $(this).attr("data-id");
    //var date = $(this).attr('value');
    $.ajax({
        url: '/materials/' + 4 +'/link/' + 16,
        data: { date: date},
        type: "GET",
        success: function(data){
            console.log(data);
            console.log(data['link']['name']);
            $('#floatingModalSignature').val(data['link']['name']);
            $('#floatingModalLink').val(data['link']['name']);
        }
    });
});


How to get the URL that was called instead of the static values ​​4 and 16 and substitute {material_id} and {link_id} in the request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav Dvoryaninov, 2021-07-19
@snake2

Laravel has a route helper. To use it, you need to give the route a name. https://laravel.com/docs/8.x/helpers#method-route Have a look.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question