簡體   English   中英

Laravel - 背包多圖像顯示在前端

[英]Laravel - Backpack multi images showing on front-end

我正在為我的 Laravel 項目使用背包。 這是我的圖片上傳代碼:我的ImageCrudController

public function setup()
    {
        /*
        |--------------------------------------------------------------------------
        | CrudPanel Basic Information
        |--------------------------------------------------------------------------
        */
        $this->crud->setModel('App\Models\Image');
        $this->crud->setRoute(config('backpack.base.route_prefix') . '/image');
        $this->crud->setEntityNameStrings('image', 'images');

        $this->crud->setColumns(['images']);
        $this->crud->addField([
            'name' => 'images',
            'label' => 'Images',
            'type' => 'upload_multiple',
            'upload' => true,
            //s'disk' => 'uploads' // if you store files in the /public folder, please ommit this; if you store them in /storage or S3, please specify it;
        ]);

        /*
        |--------------------------------------------------------------------------
        | CrudPanel Configuration
        |--------------------------------------------------------------------------
        */

        // TODO: remove setFromDb() and manually define Fields and Columns
        //$this->crud->setFromDb();

        // add asterisk for fields that are required in ImageRequest
        $this->crud->setRequiredFields(StoreRequest::class, 'create');
        $this->crud->setRequiredFields(UpdateRequest::class, 'edit');
    }

這是我的App\Models\Image model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;

class Image extends Model
{
    use CrudTrait;

    /*
    |--------------------------------------------------------------------------
    | GLOBAL VARIABLES
    |--------------------------------------------------------------------------
    */

    protected $table = 'images';
    protected $primaryKey = 'id';
    // public $timestamps = false;
    // protected $guarded = ['id'];
    protected $fillable = ['images'];
    // protected $hidden = [];
    // protected $dates = [];

    /*
    |--------------------------------------------------------------------------
    | FUNCTIONS
    |--------------------------------------------------------------------------
    */

    public function setImagesAttribute($value)
    {
        $attribute_name = "images";
        $disk = config('backpack.base.root_disk_name');;
        $destination_path = "public/uploads";

        $this->uploadMultipleFilesToDisk($value, $attribute_name, $disk, $destination_path);

    // return $this->{$attribute_name}; // uncomment if this is a translatable field
    }

    protected $casts = [
        'images' => 'array'
    ];

}

現在我正在嘗試在我的前端顯示這些圖像。只是顯示該帖子的所有圖像或顯示數組中的[0]圖像。這是我的代碼:

@foreach($images as $image)
   @foreach (json_decode($image->images) as $photo)
      <img src="uploads/{{$photo}}" style="width:50%" />
   @endforeach
@endforeach

但它向我顯示了這個錯誤:

json_decode() expects parameter 1 to be string, array given

我做錯了什么?

我不確定,但我認為您不需要使用json_decodeimages屬性已經轉換為數組。

所以你可以嘗試循環 $image->images 因為它已經是一個數組

2件事:

1-您不需要 json_decode() 2-我認為(根據您提供的內容)您應該替換

@foreach($images as $image)
   @foreach (json_decode($image->images) as $photo)
      <img src="uploads/{{$photo}}" style="width:50%" />
   @endforeach
@endforeach

@foreach($images as $image)
  <img src="uploads/{{$image}}" style="width:50%" />
@endforeach

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM