Search code examples
phplaravelimageslideshow

Laravel article image slideshow


I've got a Laravel project where you can create articles and so on. Of course I want to have an image slideshow with multiple images per article. I've managed to save multiple images in the database in one column via implode. PICTURE

How do I display the slideshow now? I have no idea.

enter image description here


Solution

  • The short answer is the one @Sang Nguyen posted: The opposite of implode() is explode().

    However I am going to add a more "Laravel-like" way of doing it:

    Assuming you have an Article model, add an accessor:

    class Article extends Model {
        ...
        public function getImagesListAttribute() {
            return collect(explode('|', $this->images));
        }
        ...
    }
    

    Then:

    $article = Article::find(1);
    
    var_dump($article->images_list); //would be a Collection of strings (which are your image names)
    

    More about accessors: https://laravel.com/docs/5.6/eloquent-mutators#defining-an-accessor

    More about collections: https://laravel.com/docs/5.6/collections