title: Laravel API APP Many-Many Relationship, how to return specific information in JSON?
body: <p>I been trying to figure this out for some time now. Basically i got 2 models ' Recipe ', ' Ingredient ' and one Controller ' RecipeController ' .
  I'm using Postman to test my API. When i go to my get route which uses RecipeController@getRecipe, the return value is as per the pic below:</p>
  
  <p><a href="https://i.stack.imgur.com/uljv6.png" rel="nofollow noreferrer">Return for Get Route</a>  </p>
  
  <p>If i want the return value of the get route to be in the FORMAT of the below pic, how do i achieve this? By this i mean i don't want to see for the recipes: the created_at column, updated_at column and for ingredients: the pivot information column, only want name and amount column information.</p>
  
  <p><a href="https://i.stack.imgur.com/xK73O.png" rel="nofollow noreferrer">Return Value Format I Want</a></p>
  
  <p>Recipe model:</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Recipe extends Model
  {
      protected $fillable = ['name', 'description'];
  
      public function ingredients()
      {
        return $this-&gt;belongsToMany(Ingredient::class, 
        'ingredient_recipes')-&gt;select(array('name', 'amount'));
      }
  }
  </code></pre>
  
  <p>Ingredient Model:</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Ingredient extends Model
  {
      protected $fillable = ['name', 'amount'];
  }
  </code></pre>
  
  <p>RecipeController</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  
  use App\Ingredient;
  use App\Recipe;
  use Illuminate\Http\Request;
  
  class RecipeController extends Controller {
  
  public function postRecipe(Request $request)
  {
      $recipe = new Recipe();
      $recipe-&gt;name = $request-&gt;input('name');
      $recipe-&gt;description = $request-&gt;input('description');
      $recipe-&gt;save();
  
      $array_ingredients = $request-&gt;input('ingredients');
      foreach ($array_ingredients as $array_ingredient) {
          $ingredient = new Ingredient();
          $ingredient-&gt;name = $array_ingredient['ingredient_name'];
          $ingredient-&gt;amount = $array_ingredient['ingredient_amount'];
          $ingredient-&gt;save();
          $recipe-&gt;ingredients()-&gt;attach($ingredient-&gt;id);
      }
  
      return response()-&gt;json(['recipe' =&gt; $recipe . $ingredient], 201);
  }
  
  public function getRecipe()
  {
      $recipes = Recipe::all();
      foreach ($recipes as $recipe) {
          $recipe = $recipe-&gt;ingredients;
      }
      $response = [
          'recipes' =&gt; $recipes
      ];
      return response()-&gt;json($response, 200);
  }
  </code></pre>
  
  <p>API Routes:</p>
  
  <pre><code>Route::post('/recipe', 'RecipeController@postRecipe')-&gt;name('get_recipe');
  Route::get('/recipe', 'RecipeController@getRecipe')-&gt;name('post_recipe');
  </code></pre>
  
  <p>Thanks Guys!</p>