简体   繁体   中英

laravel how to get JSON value from an input

I have an input item with value set as json, because I want to later get this value in controller, but I just get [object, object]

the JSON is:

    [{
    "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"
}]

Vuejs

<div v-for="article in articles_sortByDate" v-bind:key="article.id">
    <input type="checkbox"
          :value="article" 
           v-model="checkedArtcles"  
           name='checkedArtcles'>
</div>

Now if I try to get this value from controller with

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

//this results in [Object, object]

Have also tried:

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

//this return NUll

If you don't want an object, decode the string, but add true to give you an associative array:

$array = json_decode($string, true);

See here for more details http://php.net/manual/en/function.json-decode.php

在您的JavaScript逻辑(在本例中为vue)中,将json解析为字符串,然后追加到select的值,然后在控制器中,您可以尝试访问$request->input('checkedArtcles')或dd。

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