0

I have no idea where to start now. I m just getting started with Laravel and trying to seed a database with random data, using Carbon & Faker. I got this error message :

[InvalidArgumentException]
Unexpected data found.
Unexpected data found.
Unexpected data found.
Data missing

I read several topics that speak of that on SO but nothing that can help me figure out what happening. Im actually following a tutorial so everything should be ok but since I may not be on the same version of Laravel I am not sure (minors changes here and there). Here is my code for the table I try to seed (utilisateursTableSeed.php) :

 <?php

use Illuminate\Database\Seeder;
use Carbon\Carbon;

class utilisateursTableSeeder extends Seeder
{
    private function datealeatoire(){
      return Carbon::createFromDate('null', rand(1,12), rand(1,28));
    }
    /**
    * Run the database seeds.
    *
    * @return void
    */
    public function run()
    {
      DB::table('utilisateurs')->delete();

      $faker = Faker\Factory::create();

      for ($i=0; $i<20; $i++){

        $date=$this->datealeatoire();

        DB:table('utilisateurs')->insert([
          'titre'=> text(50),
          'texte'=>text(),
          'created_at'=>$date,
          'updated_at'=>$date
        ]);
      }
    }
}

My function up() looks like this :

public function up()
{
    Schema::create('utilisateurs', function (Blueprint $table) {
        $table->increments('id');
        $table->string('titre');
        $table->text('texte');
        $table->timestamps();
    });
}

I tried to change the date format but its just getting me some other errors. I have no idea what to do... please tell me if you need more info. I would appreciate any help on this

Edit: also, when im changing the 'null' value in my createFromDFate to a plain date (i.e 2017 ) I get an error saying :

 [Symfony\Component\Debug\Exception\FatalThrowableError]

 Call to undefined function table()' 

but that should not throw an error right? I mean, am I not supposed to insert any kind of date here?

DevMoutarde
  • 599
  • 7
  • 21

1 Answers1

2

You're passing null as a string instead of the datatype. Remove the quotes, and it will work:

$date = Carbon::createFromDate(null, rand(1,12), rand(1,28));
// object(Carbon\Carbon)(
//   'date' => '2017-06-02 10:28:17.000000',
//   'timezone_type' => 3,
//   'timezone' => 'America/New_York'
// )

Your next error, you're missing a second colon after DB:

DB::table('utilisateurs')->insert
aynber
  • 22,380
  • 8
  • 50
  • 63
  • Oh, here we go ! thanks for pointing that out, I was just editing my question about this. As you said, I tried writing null as it is, but I get that error "Call to undefined function table()" ! See my edit... I don't understand – DevMoutarde Feb 17 '17 at 15:31
  • I also noticed that I wrote "DB:table('utilisateurs')->insert " (as said in the tutorial) with only one ':', but there should be two ':' right? If I write DB::table, Im getting another error..... – DevMoutarde Feb 17 '17 at 15:35
  • Edited answer, you're just missing the double colon. What error after changing it? – aynber Feb 17 '17 at 15:35
  • Thanks, so now Im getting that error : [Symfony\Component\Debug\Exception\FatalThrowableError] Call to undefined function text() ??? – DevMoutarde Feb 17 '17 at 15:36
  • 2
    text() isn't a standalone function, unless you've created it somewhere. Try `$faker->text(50)` – aynber Feb 17 '17 at 15:38
  • Awww man, thats was my guess, I tried it and got the same error but didn't set $faker-> for the second text() aha. Its all good now !! It works :) thanks man ! – DevMoutarde Feb 17 '17 at 15:40