【问题标题】:Want to Show images on blade template that stored in array formet in Database on Laravel 8想要在 Laravel 8 的数据库中以数组格式存储的刀片模板上显示图像
【发布时间】:2021-07-18 19:26:44
【问题描述】:

请需要帮助。我将图像作为数组存储在数据库中。那么我如何解码刀片视图模板上的数组图像。我的控制器就是这样的

public function store(Request $req){
    $req->validate([
      'name' => 'required',
      'imageFile' => 'required',
      'imageFile.*' => 'mimes:jpeg,jpg,png,gif,csv,txt,pdf|max:2048'
    ]);

    if($req->hasfile('imageFile')) {
        foreach($req->file('imageFile') as $file)
        {
            $name = $file->getClientOriginalName();
            $file->move(public_path().'/uploads/', $name);  
            $imgData[] = $name;  
        }

        $fileModal = new Image();
        $fileModal->name = $req->input('name');
        $fileModal->image_path = json_encode($imgData);
        
       
        $fileModal->save();

       return back()->with('success', 'Images has successfully uploaded!');
    }
  }

而图像模型就是这样的......

 class Image extends Model
{
    use HasFactory;
    protected $fillable = [
        'name',
        'image_path'
    ];
 
}

我已经成功地将图像数据作为数组存储在数据库中。

但是当我使用这段代码时,它显示的是数组数据,而不是图像。

<img src="{{ URL::asset("uploads/".$photo->image_path) }}" alt="{{ $photo->name }}" style="width:150px; height:auto;">

之后,我将放置另一个代码:

@foreach ($photos as $photo)
      @php $filenames = json_decode($photo->image_path); @endphp
                
      @foreach ($filenames as $singlefilename)    
           <img src="{{ url('uploads/' . $singlefilename) }}" width="100px"/>
      @endforeach
 @endforeach

但当时它显示了包含该文件夹的所有图像,但我只需要根据 id 的行数组图像。请我需要帮助。任何人都可以帮忙吗?

【问题讨论】:

    标签: arrays laravel laravel-blade laravel-8


    【解决方案1】:

    您已经用一个名称绑定了多个 img 路径,没有键可以精确定位单个 img 路径。就像有一群没有身份的人,你怎么能找到你想要的?

    【讨论】:

    • 哎呀,这是我的错误,但现在已编辑图像路径。但是我如何将图像数组检索到字符串?
    • 从 json_decoded 数组中检索时它们不是字符串吗。
    猜你喜欢
    • 1970-01-01
    • 2022-01-11
    • 2014-09-14
    • 2016-11-20
    • 2020-08-19
    • 2018-09-05
    • 1970-01-01
    • 2021-01-07
    • 2023-01-08
    相关资源
    最近更新 更多