Nezo96-39273050's avatar

Multiple values in one column in database

Hi i want to ask for help with making column for movie genres. How would you make column for movie genre that would be able to store multiple values ? Like one movie can be Action, Komedy, Thriller and i want to ask if it is possible somethink like this: Movie: ajlskdhalksdlha Genres: [action, komedy, thriller] I know there is enum but it will store only one value from that array of values. 🤔

0 likes
6 replies
Tray2's avatar

I would advice against it, but you can use a json column for that, however, you need to be aware that it will slow down and make searching those values trickier.

I suggest giving this a read. https://tray2.se/posts/database-design

1 like
MohamedTammam's avatar

I wouldn't recommend that.

I would make a table for genre, and table for man-to-many relationship between genres and movies.

1 like
Tray2's avatar

@Nezo96-39273050 It's quite simple.

Schema::create('authors', function (Blueprint $table) {
            $table->id();
            $table->string('first_name');
            $table->string('last_name');
            $table->timestamps();
        });
    }
 Schema::create('books', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('published');
            $table->string('isbn')->nullable();
            $table->timestamps();
        });
    }
  Schema::create('author_book', function (Blueprint $table) {
            $table->id();
            $table->foreignIdFor(Author::class);
            $table->foreignIdFor(Book::class);
        });

I would suggest watching this episode, it covers many to many relations.

https://laracasts.com/series/30-days-to-learn-laravel-11/episodes/12

The whole series is highly recommended.

https://laracasts.com/series/30-days-to-learn-laravel-11

2 likes

Please or to participate in this conversation.