title: Laravel Eloquent Relationship - Inserting data into multiple tables
body: <p>I have the following structure:</p>
  
  <p>3 tables: movies, actors, genres</p>
  
  <p><strong>Movies Table Schema:</strong></p>
  
  <pre><code>Schema::create('movies', function (Blueprint $t) {
          $t-&gt;increments('id');
          $t-&gt;string('title');
          $t-&gt;integer('genre_id')-&gt;unsigned();
          $t-&gt;foreign('genre_id')-&gt;references('id')-&gt;on('genres');
          $t-&gt;string('genre');
          $t-&gt;timestamps();
      });
  </code></pre>
  
  <p><strong>Genres Table Schema:</strong></p>
  
  <pre><code>Schema::create('genres', function (Blueprint $t) {
          $t-&gt;increments('id');
          $t-&gt;string('name');
          $t-&gt;timestamps();
      });
  </code></pre>
  
  <p><strong>Actors Table Schema:</strong></p>
  
  <pre><code>Schema::create('actors', function (Blueprint $t) {
          $t-&gt;increments('id');
          $t-&gt;string('name');
          $t-&gt;timestamps();
      });
  </code></pre>
  
  <p><strong>Movie Modal:</strong></p>
  
  <pre><code>public function genre()
  {
      return $this-&gt;belongsTo('App\Genre');
  }
  public function actor()
      {
          return $this-&gt;belongsToMany('App\Actor');
      }
  </code></pre>
  
  <p><strong>Genre Modal:</strong></p>
  
  <pre><code>public function movie()
  {
      return $this-&gt;hasMany('App\Movie');
  }
  </code></pre>
  
  <p><strong>Actor Modal:</strong></p>
  
  <pre><code>public function movie()
  {
      return $this-&gt;belongsToMany('App\Movie');
  }
  </code></pre>
  
  <p><strong>Form:</strong></p>
  
  <pre><code>&lt;form method="post" action="{{ route('movies.insert') }}"&gt;
     &lt;input type="text" name="movieName" id="movieName"&gt;
     &lt;input type="text" name="actorName" id="actorName"&gt;
     &lt;input type="text" name="genre" id="genre"&gt;
     &lt;button type="submit"&gt;Add&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I am using the following controller method to post data from the form and everything is working fine but when I submit 2 movies with the same Genre, example "Action/Drama", I am getting 2 separate entries in the genres table like:</p>
  
  <p>id: 1
  name: Action/Drama</p>
  
  <p>id: 2
  name: Action/Drama</p>
  
  <p>What is the best method to use a single id for a specific genre type over and over again? For example, if I add 10 movies with genre type 'Action/Drama', then the 'genre_id' foreign key in the 'movies' table should only show one specific id which corresponds with the genres table's 'Action/Drama' Id. Hope that makes sense :/</p>
  
  <p><strong>Controller Method:</strong></p>
  
  <pre><code>public function addMovies(Request $request)
  {
      $genre = new Genre;
      $genre-&gt;name = $request-&gt;input('genre');
      $genre-&gt;save();
  
      $movie = new Movie;
      $movie-&gt;title = $request-&gt;input('movieName');
      $movie-&gt;genre = $request-&gt;input('genre');
      $movie-&gt;genre()-&gt;associate($genre);
      $movie-&gt;save();
  
      $actor = new Actor;
      $actor-&gt;name = $request-&gt;input('actorName');
      $actor-&gt;save();
      $actor-&gt;movie()-&gt;save($movie);
  
      return redirect()-&gt;route('movies.search');
  }
  </code></pre>
  
  <p><strong>The output table should look something like this:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/mcYNS.png" rel="noreferrer"><img src="https://i.stack.imgur.com/mcYNS.png" alt="enter image description here"></a></p>
  
  <p>Note: I also have a pivot table which is connecting movies with actors to facilitate the many-to-many relation, but I haven't included it here.</p>