javascript - Laravel - 在前端显示的背包多图像
问题描述
我正在为我的 Laravel 项目使用 Backpack。这是我的图片上传代码:我的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
模型:
<?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_decode
的images
属性已经转换为数组。
所以你可以尝试循环 $image->images 因为它已经是一个数组
推荐阅读
- assembly - 获取带有地址的控制流图,如程序集中所示
- python - 从 yelp 抓取评论总是返回第一页
- ansible - 在 kibana 状态端点上使用 ansible uri 模块时出错
- maxscript - 添加多个按钮和可见属性时的用户控件问题
- c# - 当安排大量 CPU 密集型工作时,.Net 线程池是否提供任何机制来避免 I/O 性能下降?
- excel - 具有多列和多行的 Vlookup
- java - Dockerize Spring Boot mongo
- matlab - matlab:内存不足,可召开会议的个人电脑
- getopts - korn 脚本选项未正确找到 getopts
- stored-procedures - 从 Talend 中的 tDBrow 调用 Snowflake 存储过程时出错