如何从数据库中检索单字段数组数据并将它们分开 - 使用 laravel、PHP

我尝试为我的产品上传多个图像,并将该图像的名称作为数组存储在数据库中。我的代码是这样工作的 -


$images = $request->file('images');      


if (isset($images)) {

  foreach($images as $image){

    $imagename = $slug.'-'.$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();

    if (!file_exists('uploads/product/images')) {

       mkdir('uploads/product/images', 0777, true);

    }

      $image->move('uploads/product/images', $imagename);

      $data[] = $imagename;

    }

            

  }else{

     $data[] = 'default.png';

  }


  $product = new Product();

  $product->images = json_encode($data);


以及存储在字段内的数据,images例如-


["jeans-2020-08-13-5f352f18b30a4.jpg","jeans-2020-08-13-5f352f18b36a0.jpg","jeans-2020-08-13-5f352f18b3a2c.jpg"]

**问题是我如何分离这个图像名称以在 Laravel Blade 中显示图像?**

或者建议我,如果有其他方法可以在 laravel-6 中上传多个图像或多个值


慕容708150
浏览 105回答 1
1回答

慕田峪9158850

您需要反序列化图像名称,然后循环它们:@foreach(json_decode($product->images) ?? [] as $image)&nbsp; &nbsp; <img src="uploads/product/images/{{ $image }}">@endforeach
打开App,查看更多内容
随时随地看视频慕课网APP