从数据库中检索所有照片

时间:2020-03-28 16:42:30

标签: php html laravel image photo

我正在建立一个分类的网站,我试图将其设置为在主页上显示数据库中的一张照片,在类别页面上显示同一张照片,但是当您查看待售物品的整页时,我想要显示该部分中的所有照片。

就像ebay如何为广告显示一张照片,但是当您单击广告时,它会显示几张照片。

我已经使用'strtok'在我的主页和类别页面中显示单张照片,但是现在我不知道如何设置查看页面以显示该项目的所有照片。

发布列表时,这是我保存图像的方式。

public function postHobbies(Request $request){
$this->validate($request, [
'photos' => 'required',
'Photos.*' => 'image|mimes:jpg, png, jpeg, gif, aae, heif, svg|max:2048'
]);

$ads = new Listings;
$images = $request->file('photos');
$count = 0;
if($request->file('photos')){
foreach($images as $item){
if($count < 6){
$var = date_create();
$date = date_format($var, 'Ymd');
$imageName = $date.'_'.$item->getClientOriginalName();
$item->move(public_path().'/uploads/',$imageName);
$url = URL::to("/").'/uploads/'.$imageName;
$arr[] = $url;
$count++;
}
}
$image = implode(",", $arr);
$ads->photos = $image;
$ads->save();
return redirect('/')->with('info', 'Listing published successfully');
}
}

这是我要在其上发布所有照片的页面的控制器

public function view(Request $request, $id){
$ads = DB::table('listings')
->select ('listings.id', 'photos', 'description', 'year', 'make', 'model', 'price', 'city', 'state', 'email')
->where(['id' => $id])
->get();
$output = '';
if($ads->count() > 0){
return view('users.posted.postedads', ['id'=>$id, 'ads'=>$ads]);
}
}
}

最后,这是我当前通过的方式,它只是向我显示单张照片,因为我仍在使用“ strtok”以确保其正常工作。所以我想我需要更改此功能,我只是不知道该使用什么或如何使用它。

<div class="row">
@if(count($ads)>0)
@foreach($ads as $row)

<div class="col-md-12">
<div class="productCard">
<img style=" width:100%;height:100%; " src=<?php echo strtok($row->photos, ',')?> />
</div>
</div>
@endforeach
@else
@endif

2 个答案:

答案 0 :(得分:0)

在没有代码的情况下很难回答,但是在您的控制器中,将它们全部放入一个数组并将其传递给您的视图。

Bitmap.CompressFormat.PNG

然后在Blade.php中,您可以像这样简单地循环遍历数组。

return view('...', compact('photo_urls');

答案 1 :(得分:0)

正如别人所说。没有代码很难回答。

但是从您的strtok函数中,我认为您有一个包含所有图像链接的字段。

您可以使用此代码来解决此问题。

$tok = strtok($row->photos, ',');

while ($tok !== false) {
    $output.= '<div class="col-md-3"><div style="text-align: center;">
            <img src=' . $tok . ' style="padding:10px !important;width:50%; height:50%;"/>
            </div></div>';
    $tok = strtok($row->photos, ',');
}

我不知道您为什么不使用explode函数。那就变得超级容易了。