【发布时间】:2021-09-28 16:04:38
【问题描述】:
我已上传存储在 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) 命令显示此内容
App\Models\Post {#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 [▶]
}
有什么建议吗?
【问题讨论】:
标签: php mysql arrays laravel-5 eloquent