title: Meal Calendar - Database Schema - Laravel
body: <p>I want to build a meal package, where I can design meals and associate them to different meals throughout the week. (breakfast or dinner from 1-7 for 7 days)</p>
  
  <p>My initial thought was a many-to-many relationship </p>
  
  <p><strong>Plan A</strong> (Don't think it'll work)</p>
  
  <p>Tables: (for example)</p>
  
  <pre><code>meals:
    id
    name
  
  package:
    id:
    name:
  
  meal_package:
    meal_id
    package_id
    calendar_code: (enum of {1D, 2D .. 7D, 1B, 2B .. 7B})
  </code></pre>
  
  <p>Classes: (as an example)</p>
  
  <pre><code>class Meal extends Model {
      public function packages() {
          return $this-&gt;belongsToMany(Package::class)-&gt;withPivot('calendar_code');
      }
  }
  
  class Package extends Model {
      public function meals() {
          return $this-&gt;belongsToMany(Meal::class)-&gt;withPivot('calendar_code');
      }
  }
  </code></pre>
  
  <p>but, attaching and detaching these will be more difficult.. a typical many-to-many wouldn't have the same 2 entities linked to each other more than once, would they?</p>
  
  <p>I'm using Laravel 5.4..</p>
  
  <p>I am open to suggestions as for other ways to approach this..</p>
  
  <p>My other thought was to change it like this.</p>
  
  <p><strong>Plan B</strong> (Don't think it's optimal..)</p>
  
  <p>Tables: (for example)</p>
  
  <pre><code>meals:
    id
    name
  
  package:
    id:
    name:
  
  meal_package:
    meal_id
    package_id
    calendar_code: (enum of {1D, 2D .. 7D, 1B, 2B .. 7B})
  </code></pre>
  
  <p>Classes: (as an example)</p>
  
  <pre><code>class Meal extends Model {
      public function package() {
          return $this-&gt;hasMany(Package::class);
      }
  }
  
  class Package extends Model {
  
      public function d1_meal() {
          return $this-&gt;belongsTo(Meal::class);
      }
  
      public function d2_meal() {
          return $this-&gt;belongsTo(Meal::class);
      }
  
      ...
  
  }
  </code></pre>
  
  <p>Thank you!</p>