PHP code example of sergeybruhin / seed-from-json
1. Go to this page and download the library: Download sergeybruhin/seed-from-json library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sergeybruhin / seed-from-json example snippets
namespace Database\Seeders;
use App\Models\Feature;
use Illuminate\Database\Seeder;
use SergeyBruhin\SeedFromJson\Traits\SeedFromJson;
class FeaturesSeeder extends Seeder
{
use SeedFromJson;
/**
* Run the database seeds.
*/
public function run(): void
{
/**
* Default base directory database_path('data');
* Default file name "data.json"
* 'features' here is relative path to /database/data/features/data.json
*/
$this
->collectFromJson('features')
->each(function (array $entry) {
$feature = Feature::where('name', $entry['name'])
->first() ?? new Feature();
$feature->name = $entry['name'];
$feature->save();
$this->logModel($feature);
});
}
}