0

This is the API that I want to get data from it

"meals": [
        {
            "idMeal": "52770",
            "strMeal": "Spaghetti Bolognese",
            "strDrinkAlternate": null,
            "strCategory": "Beef",
            "strArea": "Italian",
            "strInstructions": "Put the onion and oil in a large pan and fry over a fairly high heat for 3-4 mins. Add the garlic and mince and fry until they both brown. Add the mushrooms and herbs, and cook for another couple of mins.\r\n\r\nStir in the tomatoes, beef stock, tomato ketchup or purée, Worcestershire sauce and seasoning. Bring to the boil, then reduce the heat, cover and simmer, stirring occasionally, for 30 mins.\r\n\r\nMeanwhile, cook the spaghetti in a large pan of boiling, salted water, according to packet instructions. Drain well, run hot water through it, put it back in the pan and add a dash of olive oil, if you like, then stir in the meat sauce. Serve in hot bowls and hand round Parmesan cheese, for sprinkling on top.",
            "strMealThumb": "https://www.themealdb.com/images/media/meals/sutysw1468247559.jpg",
            "strTags": "Pasta,Meat",
            "strYoutube": "https://www.youtube.com/watch?v=-gF8d-fitkU",
            "strIngredient1": "onions",
            "strIngredient2": "olive oil",
            "strIngredient3": "garlic",
            "strIngredient4": "lean minced beef",
            "strIngredient5": "mushrooms",
            "strIngredient6": "dried oregano",
            "strIngredient7": "tomatoes",
            "strIngredient8": "hot beef stock",
            "strIngredient9": "tomato puree",
            "strIngredient10": "worcestershire sauce",
            "strIngredient11": "spaghetti",
            "strIngredient12": "parmesan",
            "strIngredient13": null,
            "strIngredient14": null,
            "strIngredient15": null,
            "strIngredient16": null,
            "strIngredient17": null,
            "strIngredient18": null,
            "strIngredient19": null,
            "strIngredient20": null,
            "strMeasure1": "2",
            "strMeasure2": "1tbsp",
            "strMeasure3": "1 clove",
            "strMeasure4": "500g",
            "strMeasure5": "90g",
            "strMeasure6": "1tsp",
            "strMeasure7": "400g can",
            "strMeasure8": "300ml",
            "strMeasure9": "1tbsp",
            "strMeasure10": "1tbsp",
            "strMeasure11": "350g",
            "strMeasure12": "Topping",
            "strMeasure13": "",
            "strMeasure14": "",
            "strMeasure15": "",
            "strMeasure16": null,
            "strMeasure17": null,
            "strMeasure18": null,
            "strMeasure19": null,
            "strMeasure20": null,
            "strSource": null,
            "strImageSource": null,
            "strCreativeCommonsConfirmed": null,
            "dateModified": null
        }
    ]
}

This is the Controller in this part I've used Http to get data from api.

<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;

class ApiController extends Controller
{
  public function index(){
        $response = Http::get('www.themealdb.com/api/json/v1/1/search.php?s=Arrabiata')->json();
        return view('foodData.meal', ['response'=>$response]);
  }
}

Route

Route::get('api', 'App\Http\Controllers\ApiController@index');

Blade File In blade file how can i Display data from api.

@foreach($response as $product)
  {{($product['strMeal'])}}
@endforeach

Error

index undefinded

Please help me out to resolve this error. Where I am struggling is to get data into the blade template. I have tried to hardcode the following and I get an undefined index error..

Timothy
  • 69
  • 7
  • 2
    nothing a `dd($product)` can't help you get more insight about. My guess is you're trying to access `strMeal` when it is inside `$product->meals[0]->strMeal`. you should dump the variable and explore it. – N69S Jan 19 '22 at 10:29
  • What N695 said. Try this for fun: `$product[0]['strMeal']` or without the loop `$response['meals'][0]['strMeal']` – user3532758 Jan 19 '22 at 10:39

1 Answers1

0

Try get data with $data = file_get_contents('www.themealdb.com/api/json/v1/1/search.php?s=Arrabiata') . Then decode it with $obj = json_decode($data) and print like $obj->strMeal

Nika Simon
  • 45
  • 1
  • 7