Rav
Rav

Reputation: 1387

Pass data from controller to modal via AJAX (Laravel)

I call a route which will generate an array from database query then I need to pass it to a modal in a view.

Controller:

$logs = DB::table....->get();
return $logs;

JS:

$('.get-logs').click(function(){
    $.ajax({
        type: 'GET',
        url: 'get-logs',
        data: {
            log_date: $t_date
        }
        success: function (data) {
           //show modal with the $logs variable
        },
    });
});

View (the button calling the AJAX function):

<a class="get-logs" href="#" name="{{ $records[$i]["row_id"] }}">{{ $records[$i]["logs_id"] }}</a>

How do I pass the $logs variable to a modal that will show when the anchor tag is clicked?

Upvotes: 0

Views: 335

Answers (1)

shahburhan
shahburhan

Reputation: 224

Assuming that your ajax call is working fine, ignoring $t_date

$logs = DB::table....->get();
return response()->json($logs, 200);

And suppose you have date, message in the log

success: function (data) {
       for(i=0; i<data.length; i++){
         $('.date_column').val(data[i].date);
         $('.message_column').val(data[i].message);
        }
    },

Upvotes: 1

Related Questions