在laravel 5.2中,我关注的是多张图片的上传,我没有使用干预。我只需要一个图像的缩略图视图和所有的图像后,点击缩略图图库。我已经成功上传了图片,但无法进入视图。我有两个表posts和images。
posts表
| id |标题描述
图像表
| id | p_id |路径
我有这样的关系。
// 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');
}
发布于 2016-11-16 11:15:36
$posts=Post::with('images')->get();
然后,这给出了第一个图像路径,您可以使用缩略图。
$posts[0]->images[0]->path;
并且,这给出了所有图像的路径。
foreach($posts as $post)
{
foreach($post->images as $image)
{
$image->path;
}
}
https://stackoverflow.com/questions/40629541
复制