0
Part of my command in laravel is failing due to a fatal error:

cannot use object of type stdClass as array

I know it has to do with the following code block, because it truncates the table but it fails on inserting new records for each row/result. Skus is a model with all the fields set as fillable.

Why would it be failing here with that error?

if ($results) {

        Skus::truncate();

        foreach ($results as $row) {
            $sku = new Skus;
            $sku->category = $row['category'];
            $sku->build = $row['build'];
            $sku->fabric = $row['fabric'];
            $sku->color = $row['color'];
            $sku->location = $row['location'];
            $sku->type = $row['type'];
            $sku->price = $row['price'];
            $sku->save();
        }

    }
1
  • var_dump $row before $row['category'] and you will probably have a better idea of what's happening. :) Commented Aug 14, 2018 at 13:57

2 Answers 2

2

Change every

$something->property = $row['somevalue']

to

$something->property = $row->somevalue

The reason is that the $row is a "class" that has some properties (variable) and when you want to access a class property you have to use "->".

Sign up to request clarification or add additional context in comments.

Comments

2

I imagine that your $results variable is part of a json response or something like that. Try decoding sending second parameter as true in order to get an array instead of an object.

$results = json_decode($results, true);

If that is not the case, please provide further info about where those results come from.

But if your error says that your array is an object, then you can access the property using the -> operator:

$sku->category = $row->category;

Comments

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.