doukeng7426 2018-11-04 03:50
浏览 21
已采纳

显示json数据以查看为String或数组

New to Laravel and feel that I'm missing something important here. I am uploading and saving multiple files to a folder and saving an array of the images names to the database(simple enough). Currently saving the images name in the json_encode() format so they are formatted like so["kittyTest.jpeg","kitty_2.jpeg","kitty_3.jpeg"]. So when I try and print them out to the view I get them in the json format and am trying to display them in an array formate or in some way that I can use the filename for the source image. Any help or guidance would be much appreciated.

Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Form;
use DB;

class FormController extends Controller
{
 public function index()
{
    $images = DB::select('select * from forms');

    //dd(json_decode($images[0]->filename));

    return view('index', ['images'=> $images]);
}

public function create()
{
    return view('create');
}

public function store(Request $request)
{
    $this->validate($request, [
        'filename' => 'required',
        'filename.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
    ]);

    if ($request->hasFile('filename')) {
        foreach ($request->file('filename') as $image) {
            $name = $image->getClientOriginalName();
            $image->move(public_path().'/images/', $name);
            $data[] = $name;
        }
    }

    $form = new Form();
    $form->filename = json_encode($data);
    $form->save();

    return back()->with('success', 'Your images have been uploaded');
}

}

View

<body>
<div class="container">


<h3 class="jumbotron">Laravel Multiple File Upload</h3>

<p>Here are the images we have in the database</p>

<ul class="list-group">
@foreach ($images as $image)
    <li class="list-group-item">
        {{ $image->filename }}
    </li>
@endforeach
</ul>
</div>


</body>
  • 写回答

2条回答 默认 最新

  • dsfsdf7852 2018-11-04 06:16
    关注
    <ul class="list-group">
    @foreach ($images as $image)
        @php $image_array = json_decode($image->filename,true); @endphp
        @foreach ($image_array as $img)
         <li class="list-group-item">
    
            {{ $img }}
         </li>
        @endforeach
    @endforeach
    </ul>
    try this and let me know
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么