laravel如何从输入中获取JSON值

时间:2017-09-20 14:02:25

标签: php json laravel vue.js

我有一个输入项,其值设置为json,因为我想稍后在控制器中获取此值,但我只是得到[object,object]

JSON是:

async Task<> DoPost(...)

Vuejs

    [{
    "title": "G30 BMW 530d is Victoria Police's new patrol car - 80 units to join fleet by end-2018, all with 'police pack'",
    "url": "https://paultan.org/2017/09/20/g30-bmw-530d-is-victoria-police-new-patrol-car/",
    "thumbnail": "https://s1.paultan.org/image/2017/09/VP_HWP_Bmw_530D_Front_side.jpg",
    "source": "Paul Tan's Automotive News",
    "source_url": "https://paultan.org/2017/09/20/g30-bmw-530d-is-victoria-police-new-patrol-car/",
    "date_published": "2017-09-20",
    "provider": "Event Registry",
    "provider_url": "http://eventregistry.org"
}, {
    "title": "Opel Astra GSi Spied Nearly Naked",
    "url": "http://gmauthority.com/blog/2017/09/opel-astra-gsi-spied-nearly-naked/",
    "thumbnail": "http://gmauthority.com/blog/wp-content/uploads/2017/07/Opel-GSi-Teaser-1024x771.jpg",
    "source": "GM Authority",
    "source_url": "http://gmauthority.com/blog/2017/09/opel-astra-gsi-spied-nearly-naked/",
    "date_published": "2017-09-19",
    "provider": "Event Registry",
    "provider_url": "http://eventregistry.org"
}]

现在,如果我尝试使用

从控制器获取此值
<div v-for="article in articles_sortByDate" v-bind:key="article.id">
    <input type="checkbox"
          :value="article" 
           v-model="checkedArtcles"  
           name='checkedArtcles'>
</div>

还尝试过:

dd($request->input('checkedArtcles'));

//this results in [Object, object]

2 个答案:

答案 0 :(得分:0)

如果您不想要对象,请对字符串进行解码,但添加true以提供关联数组:

$array = json_decode($string, true);

有关详细信息,请参阅此处http://php.net/manual/en/function.json-decode.php

答案 1 :(得分:0)

在你的javascript逻辑中(在这种情况下为vue)将你的json解析为字符串,然后附加到select的值,然后在控制器中你可以尝试访问$request->input('checkedArtcles')或dd那个。