Laravel 文章图片幻灯片
Laravel article image slideshow
我有一个 Laravel 项目,您可以在其中创建文章等。当然,我想要每篇文章包含多张图片的图片幻灯片。我已经设法通过内爆将多个图像保存在数据库中的一列中。 PICTURE
现在如何显示幻灯片?我不知道。
您只需要 explode
来自数据库的数据然后显示它
$images = explode('|', $post->image);
@foreach ($images as $image)
<img src="{{ url($image) }} alt="image" />
@endforeach
然后你可以使用 OwlCarousel https://owlcarousel2.github.io/OwlCarousel2/ 创建幻灯片。
您可以使用此示例创建基本滑块https://owlcarousel2.github.io/OwlCarousel2/demos/basic.html
示例:在您的 blade 视图中
<div class="owl-carousel owl-theme">
@foreach (explode('|', $post->image) as $image)
<div class="item"><img src="{{ url($image) }} alt="image" /></div>
@endforeach
</div>
简短的回答是@Sang Nguyen 发布的那个:implode()
is explode()
的对立面。
但是我将添加更多 "Laravel-like" 的方法:
假设您有一个 Article
模型,添加一个访问器:
class Article extends Model {
...
public function getImagesListAttribute() {
return collect(explode('|', $this->images));
}
...
}
然后:
$article = Article::find(1);
var_dump($article->images_list); //would be a Collection of strings (which are your image names)
关于访问器的更多信息:https://laravel.com/docs/5.6/eloquent-mutators#defining-an-accessor
关于集合的更多信息:https://laravel.com/docs/5.6/collections
我有一个 Laravel 项目,您可以在其中创建文章等。当然,我想要每篇文章包含多张图片的图片幻灯片。我已经设法通过内爆将多个图像保存在数据库中的一列中。 PICTURE
现在如何显示幻灯片?我不知道。
您只需要 explode
来自数据库的数据然后显示它
$images = explode('|', $post->image);
@foreach ($images as $image)
<img src="{{ url($image) }} alt="image" />
@endforeach
然后你可以使用 OwlCarousel https://owlcarousel2.github.io/OwlCarousel2/ 创建幻灯片。
您可以使用此示例创建基本滑块https://owlcarousel2.github.io/OwlCarousel2/demos/basic.html
示例:在您的 blade 视图中
<div class="owl-carousel owl-theme">
@foreach (explode('|', $post->image) as $image)
<div class="item"><img src="{{ url($image) }} alt="image" /></div>
@endforeach
</div>
简短的回答是@Sang Nguyen 发布的那个:implode()
is explode()
的对立面。
但是我将添加更多 "Laravel-like" 的方法:
假设您有一个 Article
模型,添加一个访问器:
class Article extends Model {
...
public function getImagesListAttribute() {
return collect(explode('|', $this->images));
}
...
}
然后:
$article = Article::find(1);
var_dump($article->images_list); //would be a Collection of strings (which are your image names)
关于访问器的更多信息:https://laravel.com/docs/5.6/eloquent-mutators#defining-an-accessor
关于集合的更多信息:https://laravel.com/docs/5.6/collections