LoginSignup
0
1

More than 1 year has passed since last update.

【Laravel】楽天レシピAPIからレシピを取得する方法

Last updated at Posted at 2021-10-11

はじめに

献立アプリを作る際に、楽天レシピAPIからレシピ(1カテゴリにつきランキング上位4つのレシピ)を取得したので、そのときのコードを備忘録として残しておきます。

Bladeのbodyタグ内に記述

welcome.blade.php
<body>
    @php
    $json = json_decode('APIテストフォームでパラメータ(categoryId=10といった感じ)を指定し、GETをクリックしたら出てくるデータをここにコピペ', true);

    use App\Models\Menu;
    $array = $json['result'];
    for($i=0; $i<sizeof($json['result']); $i++){
        $menu = new Menu();
        $menu->recipe_category_id = 10;
        $menu->user_id = 1;
        $menu->step = '';
        $menu->seasoning = '';
        $menu->menu_name = $array[$i]['recipeTitle'];
        $menu->image_path = $array[$i]['foodImageUrl'];
        $menu->description = $array[$i]['recipeDescription'];
        $menu->ingredient = implode(',', $array[$i]['recipeMaterial']);
        $menu->save();
    }
    @endphp
</body>

この記述を行った後、このページをリロードすると、自分で用意したテーブルのカラム内にそれぞれのデータを入れることができます。
また、IDを変えていくことで、そのIDを持つカテゴリの上位4つのレシピをどんどん取得することができます。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1