问题
In my controller i have like this
public function show(Document $document){
$doc = DB::table('adjustments')->where('document_id', $document->id)->get();
return view('documents.show',compact('document','doc'));
}
In my view i have like this
@foreach($doc as $user)
<pre>{{ $user->before}}</pre>
<pre>{{ $user->after}}</pre>
@endforeach
Im getting output like this
{"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"}
but i want show like this Title :.......... Body :......... Title :.......... Body :.........
UPDATED when i try 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"
}
]
回答1:
Do something like this:
@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
回答2:
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
回答3:
You have a json in your code now, You need to decode the variable for access rest of the code.
If your
$user->before
variable gives{"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."}
then my script must work.
Working example: https://3v4l.org/liLnr
So try with it:
@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
来源:https://stackoverflow.com/questions/37129211/how-to-display-json-data-readable-way-for-users-in-laravel