简体   繁体   中英

laravel: taking a json data from a column of a table

in Laravel I have a table named 'doors', this table has a column names 'darha'. I put json data by serialize() in this column. So I have many rows that each 'darha' column cell have data as:

a:2:{i:0;s:72:"[{ "image": "301-Door-T-N", "code": "main-1527586576301-Door-T-N.jpg" }]";i:1;s:72:"[{ "image": "402-Door-T-N", "code": "main-1527586875402-Door-T-N.jpg" }]";}

the question is How I can get the data of cell data ('image' and 'code') by clicking a button. Here is my ajax code:

function doorData(id) {
    $.ajax({
        url: '/api/apiProducts/' + id,
        method: 'get',
        dataType: 'json',
        success: function (data) {
            $.each(data, function (index, value) {
$.each(value, function (i, v) {
console.log(v.darha);
});
            });
        }
    });
}

By clicking a button I got the list of all column data, but I want to append in a div the json data inserted in a cell of 'darha' column;

You are missing data parameter.Also maybe do it like this.

$('form').on('submit',function(e){
    e.preventDefault();
    $.ajax({
        url: '/api/apiProducts/' + id,
        method: 'get',
        dataType: 'json',
        data: $('form').serialize(),
        success: function (data) {
            $.each(data, function (index, value) {
               $.each(value, function (i, v) {
                   console.log(v.darha);
               });
            });
        }
    });
});

Also a note. Maybe you should use named routes for better maintenance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM