Laravel 5 Load Seed Data from JSON

This is very useful for seeding your tables using Laravel 5 with custom data. If you want fast population look at something to do it for you like Faker for PHP. But if you want more control on the data here is how I do it. Create a ‘/database/data’ folder and put your JSON data files in there. Then simply use the same naming schema from your JSON to match your migrations. Finally, in the seeder map the data to JSON object attributes. Below is a basic example of seeding some users.


Example users.json

Now update the seeder to grab the contents of the JSON file and create the users.

Example UserTableSeeder.php

Example Migrations File.

Sam Deering

Sam Deering is a web developer from England (currently living in Australia). In his spare time he enjoys coding, playing chess, reading and investing.

6 thoughts on “Laravel 5 Load Seed Data from JSON

  1. This can be done even short by decoding the json object to an assosiative array
    $json = File::get(“database/data/articles.json”);
    $data = json_decode($json, true);
    foreach ($data as $obj) {

  2. I have error: “Trying to get property of non-object”. Whole code I wrote as you. DD function shows that in the $json and $data I have correct information (dd($data)). Can’t understand the error, I don’t have another seeds

Leave a Reply

Your email address will not be published. Required fields are marked *