如何在laravel集合中访问一组图像



我已经上传了存储在MySql数据库的数组中的图像。但我不知道如何访问laravel集合中数组的内容。我尝试的所有代码都会给我一个错误,即数组是一个字符串。

这是对下面数据库中的帖子的查询。

public function index()
{
$posts = Post::orderBy('created_at', 'desc')->paginate(10);
return view('posts.index')->with('posts', $posts);
}

这是显示张贴的刀片模板

@if (count($posts) > 0)
@foreach ($posts as $each_post)
<div class=" shadow-md bg-white pb-3  rounded-md " >
<div class="  ">
<a href="/posts/{{$each_post->slug}}"><img src="/storage/images/{{ $each_post->image}}" class=" w-full  object-fill  rounded-t-md h-44 md:h-48" alt=""></a>



</div>
<div class="p-2">
<h3 class=" text-sm md:text-lg text-gray-800 mb-2">
<a href="/posts/{{$each_post->slug}}">{{$each_post->title}}</a>
</h3>
{{-- <small> Added on: {{$each_post->created_at}} by {{ $each_post->user->name}}</small> --}}
@if ($each_post->price > 0)
<small class=" text-green-500  text-xs md:text-base"> {{$each_post->price}}  </small>
@else
<small class=" text-green-500 text-xs md:text-base"> {{'free'}}  </small>

@endif

</div>
</div>


@endforeach

</div>
{{$posts->links()}}
@else
<p>No Posts</p>
@endif

$each_post->images就是问题所在。

dd($each_post)命令显示该

AppModelsPost {#387 
#connection: "mysql"
#table: "posts"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:12 [▼
"id" => 37
"title" => "happy salah"
"slug" => "happy-salah-2"
"description" => "jkjjcjc"
"price" => "1,500"
"venue" => "talba junction"
"contact_info" => "09023234546"
"created_at" => "2021-07-20 13:49:30"
"updated_at" => "2021-07-20 13:49:30"
"user_id" => 1
"images" => "["downloadfile-2_1626788970.jpeg","downloadfile_1626788970.jpeg"]"
"author_id" => null
]
#original: array:12 [▼
"id" => 37
"title" => "happy salah"
"slug" => "happy-salah-2"
"description" => "jkjjcjc"
"price" => "1,500"
"venue" => "talba junction"
"contact_info" => "09023234546"
"created_at" => "2021-07-20 13:49:30"
"updated_at" => "2021-07-20 13:49:30"
"user_id" => 1
"images" => "["downloadfile-2_1626788970.jpeg","downloadfile_1626788970.jpeg"]"
"author_id" => null
]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}

有什么建议吗?

模型输出

IlluminateDatabaseEloquentCollection {#265 ▼
#items: array:1 [▼
0 => AppModelsPost {#266 ▼
#table: "post"
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:2 [▶]
#original: array:2 [▼
"id" => 1
"image" => "["downloadfile-2_1626788970.jpeg","downloadfile_1626788970.jpeg"]"
]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
]
}

控制器

$post = Post::all();
return view('post', ['post' => $post]);

查看

@foreach($post as $data)
@foreach(json_decode($data['image']) as $image)
{{$image}}
@endforeach
@endforeach

输出

downloadfile-2_1626788970.jpeg downloadfile_1626788970.jpeg

我已经找到了这个问题的解决方案,所以不需要提出答案。谢谢大家。

最新更新