【问题标题】:how to put my photos from database inside an array to display via explode如何将我的照片从数据库中放入数组中以通过爆炸显示
【发布时间】:2020-07-12 14:59:52
【问题描述】:

我正在尝试显示我的数据库中与存储它们的用户 ID 相关的所有照片。 如果我做这样的事情,它显然可以在数组中显示文本,但是我需要知道如何用我的图像替换该文本。

$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));

这是我存储照片的方式。它们存储在我的项目的上传文件夹以及$row->photos下的数据库中.

这就是我存储它们的方式,您可以看到它们移动到我的上传文件夹的位置。

 $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;
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]);
}
}
}

【问题讨论】:

  • 您目前用于检索照片的代码在哪里?
  • 将代码添加到帖子@Don'tPanic
  • 看起来photos 字段是一个逗号分隔的图像 URL 列表。您的 view() 代码只检索包含该字段的记录。所以这应该与您的explode() 示例完全相同?因为您在数据库查询中使用get(),所以结果是一个集合,而不是一个记录(如果您使用first(),您将只获取第一条记录,并且考虑到您通过 ID 选择应该只有 1 )。所以要获得你需要做的记录,例如$ad = $ads-&gt;first();。那么照片字段只是一个可以爆炸的字符串:print_r (explode(" ",$ad-&gt;photos));.
  • 我强烈建议您花几分钟时间阅读文档的相关部分。例如,数据库指南的Retrieving Results 部分只有 4 句话长,并且清楚地回答了您的问题,包括简单的示例代码。另请查看the Eloquent section,如果您添加一些模型和关系,您可以简化和改进您的代码。如有疑问,请阅读文档! :-)

标签: php arrays laravel explode implode


【解决方案1】:

效果很好

@foreach 

(explode(',', $row->photos) as $row->photos)
<img src="{{$row->photos}}" style="width:75%; height:75%; align-content: center;">

@endforeach

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 2019-07-26
    • 2015-11-18
    • 1970-01-01
    相关资源
    最近更新 更多