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

47 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()
{
$type = 2;
$bases = AgriculturalBase::where('type', $type)->get();
foreach ($bases as $base){
for ($i = 2019; $i < 2023; $i++) {
$crop = Crop::where('is_end', 1)->where('crop_type', $type)->inRandomOrder()->first();
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(1000, 9999),
'cultivated'=> rand(100, 999),
'output'=> rand(10000, 99999),
];
}
}
}
if(count($insertData) > 1){
DB::table('crop_yields')->truncate();
CropYield::insert($insertData);
}
}
}