简体   繁体   English

如何从laravel中的多个图像路径中获取一个图像路径?

[英]How to get just one image path from multiple images paths in laravel?

I am woring on multiple images upload in laravel 5.2 and i'm not using Intervention. 我正在考虑在laravel 5.2中上传多个图像而我没有使用干预。 I need just one image for thumbnail view and all the images after clicking the thumbnail for image gallery. 在点击图库的缩略图后,我只需要一个图像用于缩略图视图和所有图像。 I have been successfully uploading images but not been able to fetch into view.I have two tables posts and images. 我已成功上传图片但无法进入视图。我有两个表格帖子和图片。


posts table 帖子表
| | id | id | title description 标题描述
images table 图像表
| | id | id | p_id | p_id | path 路径
I have setup relationship like this. 我有这样的设置关系。

 // Post model public function images() { return $this->hasMany('App\\Models\\Image','p_id'); } //Image model public function posts() { return $this->belongsTo('App\\Models\\Post','p_id'); } 
$posts=Post::with('images')->get();

then, this gives the first image path, you can use for thumbnail. 然后,这给出了第一个图像路径,可以用于缩略图。

$posts[0]->images[0]->path; 

and, this gives all the images path. 并且,这给出了所有图像路径。

foreach($posts as $post)
{
   foreach($post->images as $image)
    {
       $image->path;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM