I'm using laravel-excel to import list product and save to database but i got error when save
foreach() argument must be of type array|object, null given
here is my code save new product in database
public function collection(Collection $rows)
{
foreach ($rows as $row) {
Product::create([
'category_id' => $row['category_id'],
'name' => $row['name'],
'slug' => Str::slug($row['slug']),
'price' => $row['price'],
'discount' => $row['discount'],
'quantity' => $row['quantity'],
'special' => $row['special'],
'published' => $row['published'],
'description' => $row['description'],
]);
}
}
I also add fillable in my model
class Product extends Model
{
use HasFactory;
protected $fillable = [
'category_id',
'slug',
'name',
'price',
'discount',
'quantity',
'special',
'description',
'published',
];
}
Please help me and why it got error!!!
Collection $rowsreturns null.That is why you are getting error.collection