2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Laravel で JSON のデータを表示

Last updated at Posted at 2018-06-07

次のプログラムを改造して、JSON を表示します。
Laravel で MariaDB のデータを表示

storage_path() というヘルバー関数を使います。

次のプログラムを差し替え

app/Models/Cities.php
<?php
// --------------------------------------------------------------------
/*
	app/Models/Cities.php

					Jun/07/2018

*/
// --------------------------------------------------------------------
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

// --------------------------------------------------------------------
class someclass
{
  var $id = "";
  var $name = "";
  var $population = "";
  var $date_mod = "";
  
  function do_something() {
    echo "メソッド内部から出力";
  }
}

// --------------------------------------------------------------------
class cities extends Model
{
  public function getData()
  {
	$url = storage_path() . '/app/public/data/cities.json';
	$json_string = file_get_contents($url);
	$dict_aa = json_decode ($json_string,true);
	$data = array();
	foreach ($dict_aa as $key => $value)
		{
		$unit_aa = new someclass();
		$unit_aa->id = $key;
		$unit_aa->name = $value["name"];
		$unit_aa->population = $value["population"];
		$unit_aa->date_mod = $value["date_mod"];
		array_push($data,$unit_aa);
		}
	return $data;
  }
}

// --------------------------------------------------------------------

次の JSON を配置

mkdir storage/app/public/data
storage/app/public/data/cities.json
{
  "t0921": {
    "date_mod": "1922-10-8",
    "name": "宇都宮",
    "population": 76381
  },
  "t0922": {
    "date_mod": "1922-8-12",
    "name": "小山",
    "population": 91237
  },
  "t0923": {
    "date_mod": "1922-3-28",
    "name": "佐野",
    "population": 23158
  },
  "t0924": {
    "date_mod": "1922-7-21",
    "name": "足利",
    "population": 51426
  },
  "t0925": {
    "date_mod": "2018-5-29",
    "name": "日光",
    "population": 342
  },
  "t0927": {
    "date_mod": "1922-7-17",
    "name": "さくら",
    "population": 46185
  },
  "t0929": {
    "date_mod": "1922-5-9",
    "name": "真岡",
    "population": 71823
  },
  "t0930": {
    "date_mod": "1922-8-12",
    "name": "栃木",
    "population": 82361
  },
  "t0931": {
    "date_mod": "1922-5-25",
    "name": "大田原",
    "population": 36128
  },
  "t0932": {
    "date_mod": "1922-4-8",
    "name": "鹿沼",
    "population": 19254
  },
  "t0933": {
    "date_mod": "1922-3-17",
    "name": "那須塩原",
    "population": 35682
  },
  "t0934": {
    "date_mod": "1922-2-21",
    "name": "那須烏山",
    "population": 24931
  }
}

他には変更はありません。

サーバーの起動

php artisan serve

ブラウザーでアクセス

http://127.0.0.1:8000/sample/model/
image.png

確認したバージョン

$ uname -a
Linux shimizu 6.2.0-32-generic #32-Ubuntu SMP PREEMPT_DYNAMIC Mon Aug 14 10:03:50 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

$ php --version
PHP 8.1.12-1ubuntu4.3 (cli) (built: Aug 17 2023 17:37:48) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.12, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.12-1ubuntu4.3, Copyright (c), by Zend Technologies

$ php artisan --version
Laravel Framework 10.22.0
2
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?