【问题标题】:How to display JSON data readable way for users in laravel?如何在 laravel 中为用户显示 JSON 数据可读方式?
【发布时间】:2016-09-04 21:09:44
【问题描述】:

在我的控制器中我有这样的

public function show(Document $document){
$doc = DB::table('adjustments')->where('document_id', $document->id)->get();
return view('documents.show',compact('document','doc'));
    }

在我看来我是这样的

@foreach($doc as $user)
<pre>{{ $user->before}}</pre>
 <pre>{{ $user->after}}</pre>
@endforeach

我得到这样的输出

{"title":"Nam beatae tempora nulla magnam accusantium neque eligendi magni.","body":"Unde doloremque omnis aspernatur atque illo quaerat. Esse nulla iure porro error est laborum. Debitis saepe pariatur voluptatem nulla non quam excepturi corporis."}
{"title":"new title","body":"new body"}

但我想要这样的节目 标题:………………正文:………… 标题:.............正文:............

更新 当我尝试 dd($doc);

 array:3 [▼
      0 => {#194 ▼
        +"id": 1
        +"user_id": 1
        +"document_id": 1
        +"before": "{"title":"Nam beatae tempora nulla magnam accusantium neque eligendi magni.","body":"Unde doloremque omnis aspernatur atque illo quaerat. Esse nulla iure porro error est laborum. Debitis saepe pariatur voluptatem nulla non quam excepturi corporis."}"
        +"after": "{"title":"new title","body":"new body"}"
        +"created_at": "2016-05-09 07:16:29"
        +"updated_at": "2016-05-09 07:16:29"
      }
      1 => {#195 ▼
        +"id": 3
        +"user_id": 1
        +"document_id": 1
        +"before": "{"body":"new body"}"
        +"after": "{"body":"new body seccond body "}"
        +"created_at": "2016-05-09 14:31:53"
        +"updated_at": "2016-05-09 14:31:53"
      }
      2 => {#196 ▼
        +"id": 4
        +"user_id": 1
        +"document_id": 1
        +"before": "{"body":"new body seccond body "}"
        +"after": "{"body":"new body seccond body jerry "}"
        +"created_at": "2016-05-09 14:32:47"
        +"updated_at": "2016-05-09 14:32:47"
      }
    ]

【问题讨论】:

  • 添加一个新维度以获取titlebody.... 例如:$user-&gt;before-&gt;title
  • 不,我试过$user-&gt;before-&gt;title 我会得到错误尝试获取非对象的属性
  • 这样做了:$user-&gt;before['title']..
  • 这也是我累的非法字符串偏移'title'
  • 查看我的更新答案。

标签: php arrays json laravel foreach


【解决方案1】:

做这样的事情:

@foreach($doc as $user)
    <div>
        <strong>Before</strong>
        Title: {{ $user->before->title }}<br />
        Body: {{ $user->before->body }}
    </div>
    <div>
        <strong>After</strong>
        Title: {{ $user->after->title }}<br />
        Body: {{ $user->after->body }}
    </div>
@endforeach

【讨论】:

  • No 当我尝试 {{ $user->before->title }} 我会得到错误 尝试获取非对象的属性
【解决方案2】:

您的代码中现在有一个 json,您需要对该变量进行解码以访问其余代码。

如果您的$user-&gt;before 变量给出{"title":"Nam beatae tempora nulla magnam accusantium neque eligendi magni.","body":"Unde doloremque omnis aspernatur atque illo quaerat. Esse nulla iure porro error est laborum. Debitis saepe pariatur voluptatem nulla non quam excepturi corporis."},那么我的脚本必须有效。

工作示例:https://3v4l.org/liLnr

那就试试吧:

@foreach($doc as $user)
    $before = json_decode($user->before);
    $after = json_decode($user->after);
    <pre>Title:{{ $before->title}}Body:{{ $before->body}}</pre>
    <pre>Title:{{ $after->title}}Body:{{ $after->body}}</pre>
@endforeach

【讨论】:

  • 不,我这样累,但我得到错误试图获取非对象的属性
  • 请提供完整的$doc
  • 是的,我试过这样$doc = DB::table('adjustments')-&gt;where('document_id', $document-&gt;id)-&gt;get(); foreach($doc as $d){ $data= $d-&gt;before; $d = json_decode($data,true); var_dump($d); //return view('documents.show',compact('document','d')); }所以我就这样出去了
  • array(2) { ["title"]=&gt; string(65) "Nam beatae tempora nulla magnam accusantium neque eligendi magni." ["body"]=&gt; string(162) "Unde doloremque omnis aspernatur atque illo quaerat. Esse nulla iure porro error est laborum. Debitis saepe pariatur voluptatem nulla non quam excepturi corporis." } array(1) { ["body"]=&gt; string(8) "new body" } array(1) { ["body"]=&gt; string(22) "new body seccond body " }
  • 因为true中的json_decode($data,true),所以你得到了关联数组,如果你不使用它,你会得到数组对象。
【解决方案3】:
try this :
@foreach($doc as $user)
$before = json_decode($user->before);
$after = json_decode($user->after);
<pre>Title:{{ $before['title'] }}Body:{{ $before['body'] }}</pre>
<pre>Title:{{ $after['title'] }}Body:{{ $after['body'] }}</pre>
@endforeach

【讨论】:

  • 我之前在这一行试过 $before = json_decode($user->before);错误未定义变量,因为在 laravel 视图中 $user-> 在它们之前没有输出 :(
猜你喜欢
  • 1970-01-01
  • 2014-01-13
  • 1970-01-01
  • 2020-06-03
  • 2019-02-23
  • 2018-04-26
  • 2020-10-10
  • 2020-10-25
  • 2019-01-25
相关资源
最近更新 更多