如何在laravel中为用户显示JSON数据可读方式?

时间:2016-05-10 04:20:57

标签: php arrays json laravel foreach

在我的控制器中我喜欢这个

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

答案 2 :(得分:-1)

您的代码中现在有一个json,您需要解码变量以获取代码的访问权限。

  

如果您的$user->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