title: Save into many to many relationship laravel
body: <p>i have some troubles when i try to save into a manytomany's relationship
  basically i don't know how to do that , i only can do it with a simple values from the same table
  I have in my database pizzas , ingredients and the pivot table ingredient_pizza.
  I want to save some ingredients for one pizza that means an array of ingredients.
  I don't have problems when i read ingredients from pizza , but i have problems when i try to save into the database. </p>
  
  <p>Migrations</p>
  
  <pre><code>public function up()
  {
      Schema::create('pizzas', function(Blueprint $table)
      {
          $table-&gt;increments('id');
          $table-&gt;string('pizza_name');
          $table-&gt;rememberToken();
          $table-&gt;timestamps();
      });
  }
  
  
  
  
  public function up()
  {
      Schema::create('ingredients', function(Blueprint $table)
      {
          $table-&gt;increments('id');
          $table-&gt;string('ingredient_name');
          $table-&gt;rememberToken();
          $table-&gt;timestamps();
      });
  }
  
  
  
  public function up()
  {
      Schema::create('ingredient_pizza', function(Blueprint $table)
      {
          $table-&gt;increments('id');
  
          $table-&gt;integer('pizza_id')-&gt;unsigned();
          $table-&gt;foreign('pizza_id')-&gt;references('id')-&gt;on('pizzas');
  
          $table-&gt;integer('ingregient_id')-&gt;unsigned();
           $table-&gt;foreign('ingregient_id')-&gt;references('id')-&gt;on('ingregients');
  
          $table-&gt;rememberToken();
          $table-&gt;timestamps();
      });
  }
  </code></pre>
  
  <p>Pizza's model</p>
  
  <pre><code>public function ingredients()
  {
      return $this-&gt;belongsToMany('App\Ingredient');
  }
  </code></pre>
  
  <p>Ingredient's model</p>
  
  <pre><code>public function Pizza()
  {
      return $this-&gt;belongsToMany('App\Pizza');
  }
  </code></pre>
  
  <p>And i set 2 ingredients for pizza1 by phpmyadmin , and pizza2 doesn't have ingredients</p>
  
  <pre><code>//it works , 
  $pizza1 = Pizza::find(1);
  foreach ($pizza1-&gt;ingredients as $ingredient){
      echo $ingredient-&gt;ingredient_name;   
  }
  
  
  //it works too ,it shows two ingredients and basically i set the same ingredients for pizza2
  $pizza2 = Pizza::find(2);
  $pizza2-&gt;ingredients=$pizza1-&gt;ingredients;
  foreach ($pizza2-&gt;ingredients as $ingredient){
      echo $ingredient-&gt;ingredient_name;   
  }
  </code></pre>
  
  <p>but if i want to save the ingredients for pizza2 I have troubles 
  i tried save in many ways but it didn't work</p>
  
  <pre><code>$pizza2-&gt;save;
  $pizza2-&gt;save();
  $pizza2-&gt;saveMany;
  $pizza2-&gt;saveMany();
  $pizza2-&gt;ingredients-&gt;save;
  </code></pre>
  
  <p>if you can give an example , i would like to use new ingredients .
  I used the same ingredients because i don't know how can i make new ingredients , save in an array and set into pizza at the same time</p>