Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

295
Views
Problema al llenar una fecha aleatoria con seeder Laravel 6

Estoy llenando una base de datos con seeders y factory, el problema es que necesito llenar el campo CREATED_AT con fechas que no son de hoy sino aleatorias, para poder llenar las diferentes gráficas que tiene la página.

Probé y a veces me inserta datos y otras veces me tira error de que el campo no es válido y que está en formato de fecha el error que me tira en la consola.

 ERROR MESSAGE "Incorrect datetime value: '2021-09-05 00:00:00' for column 'created_at' at row 1"

imagen de error

CÓDIGO

 $factory->define(Opportunity::class, function (Faker $faker) { $account_id = Account::all()->random()->id; $account = Account::find($account_id); $contact = Contact::where('account_id',$account_id)->inRandomOrder()->limit(1)->first(); $created = $this->faker->dateTimeBetween($startDate = '-3 month', $endDate = 'now +6 month'); $date = strtotime('+2 days', strtotime(Carbon::parse($created))); return [ 'created_at' => Carbon::parse($created)->format('Ymd H:i:s'), ///line error 'name' => $this->faker->name .' '.$this->faker->sentence(2), 'amount' => $this->faker->numberBetween($min = 120000, $max = 20000000), 'probability' => $faker->randomElement(['0','10','20','30','40','50','60','70','80','90','100']), 'description' => $this->faker->paragraph, 'lead_source_id'=> LeadSource::all()->random()->id, 'sales_stage_id'=> 1, 'account_id' => $account_id, 'user_id' => $account->user_id, 'contact_id' => ( $contact != null ? $contact->id : null), 'close_date' => Carbon::parse($date)->format('Ym-d'), 'product_line_id'=> ProductLine::all()->random()->id ]; });
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Intente cambiar $this->faker a $faker y la migración debería tener $table->timestamps();

entonces puedes usar dateTimeBetween directamente así

 'created_at'=>$faker->dateTimeBetween($startDate = '-3 month',$endDate = 'now +6 month')

Clase de sembrador de oportunidades

 public function run() { factory(App\Opportunity::class,5)->create(); }

Clase DatabaseSeeder

 $this->call([ OpportunitySeeder::class ]);

https://laravel.com/docs/6.x/database-testing#using-seeds

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!