atieh mokhtary atieh mokhtary - 3 months ago 16
MySQL Question

create ManyToMany relation or an array of movieId in laravel 5.2?

I am going to create a website which has exactly same as IMDB.com site logic.

I have

lists
table that can have lots of movies from
movies
table. now I want to know which method is the best choice?


  1. Creating a
    ManyToMany
    relation between
    lists
    and
    movies
    table and create a pivot table to store ids from both tables.

  2. In
    lists
    table create a field named
    movieId
    and store all movies ids like an array in this field and separate them with ','.



Please let me know what way would you choose and why?

Answer

Ok. Go for ManyToMany Relationship. Eloquent already has the mechanism to handle this relationship reducing your effort of writing code. Defining the proper relation via pivot table will make you much more easier for handling CRUD operation too.

With the second approach you won't be able to handle this using Eloquent.And again this is not the standard way of creating relationship between tables. Besides you have to manually prepare the name of the movies separated by comma.

Comments