LoginSignup
2
4

More than 5 years have passed since last update.

【Laravelメモ⑦】Google Books APIで検索した書籍情報を表示する

Posted at

前提

  • あるページの入力フォームから検索用の文字列を入力
  • Google Books APIで本の情報を取得
    • とりあえず、本のIDをforeachで出力する

メモ

入力ビュー

input.blade.php
<form action="result" method="post">
<input type="text" name="book" size="20"/>
<input type="submit" value="Submit"/>
</form>

ルーティング

web.php
Route::post('result','testController@result');

コントローラ

testController.php
class testController extends Controller
{
    public function result(Request $request){
        $post_data = $request->all();
        $data = "https://www.googleapis.com/books/v1/volumes?q=".$post_data["book"];
        $json = file_get_contents($data);
        $json_decode = json_decode($json, true);
        return view('result', compact("json_decode"));
    }
}

出力ビュー

result.blade.php
@foreach ($json_decode['items'] as $item)
    {{ $item['id'] }}
@endforeach
2
4
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
2
4