lcly-data-admin/database/seeders/CropYieldSeeder.php

46 lines
1.3 KiB
PHP

<?php
namespace Database\Seeders;
use App\Models\Crop;
use App\Models\CropYield;
use Illuminate\Database\Seeder;
use App\Models\AgriculturalBase;
use Illuminate\Support\Facades\DB;
class CropYieldSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$crops = Crop::where('is_end', 1)->where('crop_type', 1)->get();
$insertData = [];
foreach ($crops as $crop) {
$base = AgriculturalBase::where('type', 1)->inRandomOrder()->first();
for($i = 2019; $i < 2023; $i++){
for($j = 1; $j< 5; $j++) {
$insertData[] = [
'base_id' => $base->id,
'crop_id' => $crop->id,
'category_id' => $crop->category_id,
'time_year' => $i,
'quarter' => $j,
'yield' => rand(10000, 99999),
'cultivated'=> rand(100, 999),
'output'=> rand(100000, 999999),
];
}
}
}
if(count($insertData) > 1){
DB::table('crop_yields')->truncate();
CropYield::insert($insertData);
}
}
}