1

json data:

 {"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
    }
  ]
}

And I have a table named province & have columns named: name, lat, long

How can I add this data to my table as name and lat and long in Laravel?

my table:

public function up()
    {
        Schema::create('provinces', function (Blueprint $table) {
            $table->integer('id');
            $table->string('name');
            $table->text('lat');
            $table->text('long');
            $table->timestamps();
        });
    }
3
  • use json_decode() function to convert it to array then mass assign it to laravel eloquent Laravel Mass Assignment Commented Sep 19, 2016 at 5:46
  • I think you somehow need to tell us HOW you want to achieve this. Do you want to seed your database with this data? Or do you receive data like this more often and need a way to import json? or is it an on the fly process where you request json data and want to insert it into the database? Commented Sep 19, 2016 at 6:35
  • I just want to seed database with this data Commented Sep 19, 2016 at 17:24

1 Answer 1

3

try this one,

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

or

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

Note:- You need not to give id value if it is auto increments column means.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.