0

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!!!

2
  • 1
    The problem is that Collection $rows returns null.That is why you are getting error. Commented Jun 7, 2024 at 11:09
  • To expand on @Subha's comment, the code currently posted has nothing to do with the actual issue. Please revise and post the actual code responsible for setting/initializing the variable you are passing to the function collection Commented Jun 7, 2024 at 11:40

1 Answer 1

-3
  1. please try to modify the collection method and put this code
public function collection(Collection $rows)
{
    foreach ($rows as $row) {
        if (is_null($row['category_id'])) {
            continue; // Skip empty rows
        }

        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'],
        ]);
    }
}

  1. make sure import excel library
use Maatwebsite\Excel\Facades\Excel;
Excel::import(new ProductsImport, 'products.xlsx');
Sign up to request clarification or add additional context in comments.

2 Comments

How would this answer the question? OP is using the package laravel-excel not Maatwebsite\Excel\Facades\Excel
FYI: "Try this" is not an answer. Please explain how this actually would solve the issue and please don't use snippets for non-run-able code. (It doesn't because the problem is not in the posted code as pointed out in the comment section)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.