使用Laravel将Json数组写入数据库

时间:2015-08-16 22:10:44

标签: php json laravel

我在$ config中包含如下数组:

Array
(
    [1] => Array
        (
            [outlet] => facebook
            [thumbnail] => /images/catalog/modules/videoplayer/Original.jpg
            [url] => http://www.google.com
            [copy] => this is a test
            [vidurl] => http://www.youtube.com
            [poster] => none
        )

    [2] => Array
        (
            [outlet] => facebook
            [thumbnail] => /images/catalog/modules/videoplayer/limon2.jpg
            [url] => http://www.yahoo.com
            [copy] => Here is the copy
            [vidurl] => http://www.vimeo
            [poster] => no poster
        )

)

我将其转换为JSON,如下所示:

$module->configuration = json_encode($config,JSON_PRETTY_PRINT);

所有内容都保存到数据库中,在检索时生成此对象:

App\Module Object
(
    [table:protected] => modules
    [timestamps] => 
    [connection:protected] => 
    [primaryKey:protected] => id
    [perPage:protected] => 15
    [incrementing] => 1
    [attributes:protected] => Array
        (
            [id] => 3
            [title] => wddwdw
            [type] => socialfeed
            [configuration] => {
    "1": {
        "outlet": "facebook",
        "thumbnail": "\/images\/catalog\/admin\/no_image.png",
        "url": "wdwd",
        "copy": "wd",
        "vidurl": "wdw",
        "poster": "wd"
    },
    "2": {
        "outlet": "facebook",

        )

    [original:protected] => Array
        (
            [id] => 3
            [title] => wddwdw
            [type] => socialfeed
            [configuration] => {
    "1": {
        "outlet": "facebook",
        "thumbnail": "\/images\/catalog\/admin\/no_image.png",
        "url": "wdwd",
        "copy": "wd",
        "vidurl": "wdw",
        "poster": "wd"
    },
    "2": {
        "outlet": "facebook",

        )

    [relations:protected] => Array
        (
        )

    [hidden:protected] => Array
        (
        )

    [visible:protected] => Array
        (
        )

    [appends:protected] => Array
        (
        )

    [fillable:protected] => Array
        (
        )

    [guarded:protected] => Array
        (
            [0] => *
        )

    [dates:protected] => Array
        (
        )

    [dateFormat:protected] => 
    [casts:protected] => Array
        (
        )

    [touches:protected] => Array
        (
        )

    [observables:protected] => Array
        (
        )

    [with:protected] => Array
        (
        )

    [morphClass:protected] => 
    [exists] => 1
)

你可以看到json坏了。当我检索$ model->配置时,它被破坏了:

{
    "1": {
        "outlet": "facebook",
        "thumbnail": "\/images\/catalog\/admin\/no_image.png",
        "url": "wdwd",
        "copy": "wd",
        "vidurl": "wdw",
        "poster": "wd"
    },
    "2": {
        "outlet": "facebook",

在将json写入数据库以确保它是可检索的之前,我还需要做些什么吗?

1 个答案:

答案 0 :(得分:1)

就像评论中的@Scuzzy说的那样,我认为我们这里有一个长度问题。要么你使用varchar(255)将这个JSON存储在你的数据库中,那就是它被切断的原因,因为你的字符串太长了或者它只是你的var_dump或print didn& #39; t显示所有内容,你的对象很好。

我认为这可能是第一种情况,我建议将其存储为数据库中的序列化数组而不是JSON。