2016-09-19 40 views
1

JSON數據:如何包括JSON數據到MySQL在Laravel

{"RECORDS": [{ 

     "id": 1, 
     "name": "آذربایجان شرقی", 
     "lat": 37.90357330, 
     "long": 46.26821090 
    }, 
    { 
     "id": 2, 

     "name": "آذربایجان غربی", 
     "lat": 37.45500620, 
     "long": 45.00000000 
    }, 
    { 
     "id": 3, 

     "name": "اردبیل", 
     "lat": 38.48532760, 
     "long": 47.89112090 
    }, 
    { 
     "id": 4, 

     "name": "اصفهان", 
     "lat": 32.65462750, 
     "long": 51.66798260 
    }, 
    { 
     "id": 5, 

     "name": "البرز", 
     "lat": 35.99604670, 
     "long": 50.92892460 
    }, 
    { 
     "id": 6, 

     "name": "ایلام", 
     "lat": 33.29576180, 
     "long": 46.67053400 
    }, 
    { 
     "id": 7, 

     "name": "بوشهر", 
     "lat": 28.92338370, 
     "long": 50.82031400 
    }, 
    { 
     "id": 8, 

     "name": "تهران", 
     "lat": 35.69611100, 
     "long": 51.42305600 
    }, 
    { 
     "id": 9, 

     "name": "چهارمحال و بختیاری", 
     "lat": 31.96143480, 
     "long": 50.84563230 
    }, 
    { 
     "id": 10, 

     "name": "خراسان جنوبی", 
     "lat": 32.51756430, 
     "long": 59.10417580 
    }, 
    { 
     "id": 11, 

     "name": "خراسان رضوی", 
     "lat": 35.10202530, 
     "long": 59.10417580 
    }, 
    { 
     "id": 12, 

     "name": "خراسان شمالی", 
     "lat": 37.47103530, 
     "long": 57.10131880 
    }, 
    { 
     "id": 13, 

     "name": "خوزستان", 
     "lat": 31.43601490, 
     "long": 49.04131200 
    }, 
    { 
     "id": 14, 

     "name": "زنجان", 
     "lat": 36.50181850, 
     "long": 48.39881860 
    }, 
    { 
     "id": 15, 

     "name": "سمنان", 
     "lat": 35.22555850, 
     "long": 54.43421380 
    }, 
    { 
     "id": 16, 

     "name": "سیستان و بلوچستان", 
     "lat": 27.52999060, 
     "long": 60.58206760 
    }, 
    { 
     "id": 17, 

     "name": "فارس", 
     "lat": 29.10438130, 
     "long": 53.04589300 
    }, 
    { 
     "id": 18, 

     "name": "قزوین", 
     "lat": 36.08813170, 
     "long": 49.85472660 
    }, 
    { 
     "id": 19, 

     "name": "قم", 
     "lat": 34.63994430, 
     "long": 50.87594190 
    }, 
    { 
     "id": 20, 

     "name": "كردستان", 
     "lat": 35.95535790, 
     "long": 47.13621250 
    }, 
    { 
     "id": 21, 

     "name": "كرمان", 
     "lat": 30.28393790, 
     "long": 57.08336280 
    }, 
    { 
     "id": 22, 

     "name": "كرمانشاه", 
     "lat": 34.31416700, 
     "long": 47.06500000 
    }, 
    { 
     "id": 23, 

     "name": "کهگیلویه و بویراحمد", 
     "lat": 30.65094790, 
     "long": 51.60525000 
    }, 
    { 
     "id": 24, 

     "name": "گلستان", 
     "lat": 37.28981230, 
     "long": 55.13758340 
    }, 
    { 
     "id": 25, 

     "name": "گیلان", 
     "lat": 37.11716170, 
     "long": 49.52799960 
    }, 
    { 
     "id": 26, 

     "name": "لرستان", 
     "lat": 33.58183940, 
     "long": 48.39881860 
    }, 
    { 
     "id": 27, 

     "name": "مازندران", 
     "lat": 36.22623930, 
     "long": 52.53186040 
    }, 
    { 
     "id": 28, 

     "name": "مركزی", 
     "lat": 33.50932940, 
     "long": -92.39611900 
    }, 
    { 
     "id": 29, 

     "name": "هرمزگان", 
     "lat": 27.13872300, 
     "long": 55.13758340 
    }, 
    { 
     "id": 30, 

     "name": "همدان", 
     "lat": 34.76079990, 
     "long": 48.39881860 
    }, 
    { 
     "id": 31, 
     "name": "یزد", 
     "lat": 32.10063870, 
     "long": 54.43421380 
    } 
    ] 
} 

而且我有一個表命名省&已經列命名namelatlong

哪有我將這些數據添加到我的表中,分別爲namelatlong在Laravel?

我的表:

public function up() 
    { 
     Schema::create('provinces', function (Blueprint $table) { 
      $table->integer('id'); 
      $table->string('name'); 
      $table->text('lat'); 
      $table->text('long'); 
      $table->timestamps(); 
     }); 
    } 
+0

使用json_decode()函數將其轉換爲數組然後將其分配給laravel雄辯[Laravel Mass Assignment](https://laravel.com/docs/5.3/eloquent#mass-assignment) –

+0

我認爲你需要某種方式告訴我們你是如何實現這一目標的。你想用這些數據爲你的數據庫播種嗎?或者你更頻繁地收到這樣的數據,並需要導入json的方法?或者它是一個即時的過程,您在哪裏請求json數據並希望將其插入到數據庫中? –

+0

我只是想用這些數據來種子數據庫 – Katerou22

回答

3

試試這個,

 foreach($data as $d){ 
     Province::create($d); 
    }  

 foreach($data['RECORDS'] as $d){ 
     Province::create($d); 
    }   

注: - 你不必給的值id如果是自動列增加列的意思。