@denis
To fetch data from a JSON with Laravel, you can follow these steps:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public function fetchDataFromJsonFile() { // Path to the JSON file $file = public_path('data.json'); // Read the contents of the file $jsonString = file_get_contents($file); // Convert the JSON string into a PHP array $data = json_decode($jsonString, true); // Access the data and do something with it foreach ($data as $item) { echo $item['name'] . ' - ' . $item['email'] . '<br>'; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
use IlluminateSupportFacadesHttp; public function fetchDataFromApi() { // Make a GET request to the API $response = Http::get('https://api.example.com/data'); // Convert the JSON response into a PHP array $data = $response->json(); // Access the data and do something with it foreach ($data as $item) { echo $item['name'] . ' - ' . $item['email'] . '<br>'; } } |
By following these steps, you can fetch data from a JSON file or API in your Laravel application.