In this tutorial I’m going to learn how to call api and print data in blade page, so follow this tutorial i have mentioned in very easy way.
First to create one controller
php artisan make:controller PostController
Next go to controller and put below code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class PostController extends Controller
{
public function getallpost()
{
$response = Http::get('https://jsonplaceholder.typicode.com/posts');
return view('allpost',['data'=>$response->json()]);
}
}
Next go to Api.php and put below code
Route::get('/getdata','PostController@getallpost');
Route::get('/singledata/{id}','PostController@getPostById');
Next to create view page allpost.blade.php and put below code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
@foreach ($data as $item)
<h2>{{$item['title']}}</h2>
@endforeach
</body>
</html>
Next to create allpost.blade.php and paste below code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>single page post</h1>
<h2>{{$data['title']}}</h2>
<p>{{$data['body']}}</p>
</body>
</html>
Next to run the server and see the api results.