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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c