Suppose we have created JSON file for particular task like country list and we need to use it at multiple location and process some operations like sorting, searching, filtering etc. So we can handle it more easily and save processing time with databases instead of files.
But converting JSON Data into database is hard process only if we approach manually. Here in Laravel we can easily add those data into table with seeder programmatically whenever required.
Let's assume your application is running and database is configured.
In this application, First of all we need JSON file which contains country names and code. You can download online or create new one. You can add this file at any location.
Here we will create new directory into database directory by JSON name and move our JSON file in it.
To store JSON data from file we need a model. So let's create model using below command:
php artisan make:model CountryCode -m
It will create CountryCode.php at App\Models Directory.
app\Models\CountryCode.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class CountryCode extends Model
{
use HasFactory;
protected $fillable = [
'name',
'dial_code',
'code',
];
}
While creating we have passed -m so it will create migration file with it. Open migration file and make relevant changes.
database/migrations/timestamp_country_codes_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCountryCodesTable extends Migration
{
public function up()
{
Schema::create('country_codes', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('dial_code');
$table->string('code');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('country_codes');
}
}
As per migration we have created three new columns besides id, created_at and updated_at columns: name, dial_code and code.
To run migration execute the given below command into terminal:
php artisan migrate
It will generate database table by name country_codes.
In this step, we will create seeder for our model and write logic to read JSON file and insert JSON file data into our database. To create sedder enter below command into terminal :
php artisan make:seeder CountryCodeSeeder
The above command will create database/seeders/CountryCodeSeeder.php file into your application. Let's make some as per requirement :
<?php
namespace Database\Seeders;
use App\Models\CountryCode;
use Illuminate\Database\Seeder;
use File;
class CountryCodeSeeder extends Seeder
{
public function run()
{
CountryCode::truncate();
$json_data = File::get("database/Json/CountryCodes.json");
$countries = json_decode($json_data);
foreach ($countries as $key => $value) {
CountryCode::create([
"name" => $value->name,
"dial_code" => $value->dial_code,
"code" => $value->code
]);
}
}
}
As per logic, first of all it will read JSON file using File. Then it will use json_decode() function to convert JSON data into array so we can loop through it. At last it will create records using loop into out CountryCodes model.
We can directly run our seeder but setting our seeder into Database Seeder is best practice in enterprise application. So will register it into our DatabaseSeedder :
database/seeders/DatabaseSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call([
CountryCodeSeeder::class,
]);
}
}
Finally, We have created out seeder and it's time to seed our database. Enter below command into terminal :
php artisan db:seed
If you don't want to register it into Database Seeder then you can simply run below command into your terminal and seed data :
php artisan db:seed --class=CountryCodeSeeder
Note :Sometimes after creating a new seeder you face an error, so you might require to regenerate Composer’s autoloader.:
Ask anything about this examples