I have a problem when creating data with the Maatwebsite/excel
import. In my import class, I want to create data with the relation, when I try the dd
variable that contains child data its works just fine, but when I saveMany()
the data returns null and error.
Integrity constraint violation: 1048 Column ‘description’ cannot be null
Here is my code:
public function collection(Collection $rows) { $parent = Parent::create([ 'name' => $this->name, 'slug' => $this->slug, 'month' => $this->month, ]); foreach ($rows as $row) { $item = new Child([ 'description' => $row['description'], 'status' => $row['status'], ]); $items[] = $item; } $parent->childs()->saveMany($items); }
Advertisement
Answer
Probabily your $row[‘description’] in foreach have null value.
Edit you loop code like this
foreach ($rows as $row) { if(!empty($row['description])) { $item = new Child([ 'description' => $row['description'], 'status' => $row['status'], ]); $items[] = $item; } }
And also edit the last line
if(!empty($items)) $parent->childs()->saveMany($items);