【问题标题】:laravel how to get JSON value from an inputlaravel如何从输入中获取JSON值
【发布时间】:2018-03-01 15:23:12
【问题描述】:

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

JSON 是:

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

现在,如果我尝试使用

从控制器获取此值
dd($request->input('checkedArtcles'));

//this results in [Object, object]

也试过了:

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

//this return NUll

【问题讨论】:

  • 请提供工作示例。在 sn-p 中进行现场演示?

标签: php json laravel vue.js


【解决方案1】:

如果你不想要一个对象,解码字符串,但是添加true给你一个关联数组:

$array = json_decode($string, true);

更多详情请看这里http://php.net/manual/en/function.json-decode.php

【讨论】:

  • 这个 JSON 字符串是从哪里来的?
  • json 字符串来自 vue.js 组件中的数据
  • 在控制器中抓取它的任何地方都将其回显并 var 转储它以确保您正确获取字符串
  • echo 也只是返回 [object Object]
【解决方案2】:

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

【讨论】:

  • 我认为问题在于 $request->input('') 正在将 json 更改为字符串,从而获取对象,对象。我已经尝试过您的解决方案,它返回错误 Call to a member function toArray() on string
  • 所以这意味着 $request->input('checkedArtcles') 是一个字符串,你能告诉我 vue 生成的源代码吗? ctrl+u 在你的页面上显示选择,可能可能是因为它的值已经带有值'object'的字符串。
  • 你可能在这里是我在课程中得到的[object Object],[object Object],[object Object] "
  • 是的,我已经这样做了,但现在的问题是该值不适合 json_decode 我可以将其视为字符串 "{"title":"Peugeot-Citroen 与 GM 达成交易像这样购买 Vauxhall-Opel","url":"p.dw.com/p/2YgXx","thumbnail":"https://…" 但 json_decode 返回 null
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-14
  • 1970-01-01
相关资源
最近更新 更多