2

I want to be able to save many rows in a row in a table, i have read that this can be done using the create method, but I am getting this error when trying to using the method

SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value (SQL: insert into person_roles (updated_at, created_at) values (2019-01-30 07:46:19, 2019-01-30 07:46:19))

my code

    $data = [
        ['name' => 'actor'],
        ['name' => 'translator'],
        ['name' => 'director'],
        ['name' => 'stageDirector'],
        ['name' => 'costume'],
        ['name' => 'musical'],
        ['name' => 'supportDirector'],
        ['name' => 'photographer'],
        ['name' => 'choreographer'],
    ];

    $personRoles = PersonRole::create($data);

I my using Laravel v5.7.21

Updated, inside my PersonRole i have added this line

protected $fillable = ['name'];

in order for the mass assigning to work, but I am still getting the error.

Dimitar
  • 1,830
  • 5
  • 32
  • 52

3 Answers3

4

You must be use insert() method, but when you use insert method you must be manually insert created_at, and updated_at columns

$data = [
    [
        'name' => 'actor',
        'created_at' => now(),
        'updated_at' => now(),
    ],
    ['name' => 'translator'],
    ['name' => 'director'],
    ['name' => 'stageDirector'],
    ['name' => 'costume'],
    ['name' => 'musical'],
    ['name' => 'supportDirector'],
    ['name' => 'photographer'],
    ['name' => 'choreographer'],
];

$personRoles = PersonRole::insert($data);
Davit Zeynalyan
  • 8,418
  • 5
  • 30
  • 55
1

UPDATE :

in your case

foreach($data as $datas){PersonRole::create(['name'=>$datas->name])}

or

$now = Carbon::now()->toDateTimeString();
PersonRole::insert([
    ['name'=>'Foo', 'created_at'=>$now, 'updated_at'=>$now],
    ['name'=>'Bar', 'created_at'=>$now, 'updated_at'=>$now],
    ['name'=>'Baz', 'created_at'=>$now, 'updated_at'=>$now],
    ..................................
]);

or make function in model class and call it

public function createManyRecord(array $records){
    foreach ($records as $record) {
        $this->create($record);
    }
    return $this;
}
mp.hamid
  • 76
  • 4
  • but `createMany` function is when there is a relationship, like in your demo, there is a relationship between post and comments, but i don't have any relationship, so i don't think this is the right and working choose!? – Dimitar Jan 30 '19 at 08:02
  • `createMany` will not work if there is no relashipship, like in my case – Dimitar Jan 30 '19 at 08:03
  • in your case **PersonRole::createMany($data);** – mp.hamid Jan 30 '19 at 08:06
  • this is giving me an error `Call to undefined method App\PersonRole::createMany()` – Dimitar Jan 30 '19 at 08:07
  • 1
    The only way to use the **foreach** in data or do **foreach** in model functions – mp.hamid Jan 30 '19 at 08:19
1

You have to use insert instead of create-

$personRoles = PersonRole::insert($data);
Davit Zeynalyan
  • 8,418
  • 5
  • 30
  • 55
Jitendra
  • 584
  • 1
  • 10
  • 28