sanjayacloud's avatar

Trying to access array offset on value of type null

Hi everyone,

I am trying to migrate data from the old site to the new site. When I try to upload images from the old site to the new site. It will give me a "Trying to access array offset on value of type null" error. Anyone can help me to resolve this error.?

Here is my controller method"

public function imageMigration()
    {

        $images = DB::connection('aegis_tech')->table('entity_files')->where('entity_type', '=','Modules\Product\Entities\Product')->get();
        foreach ($images as $image)
        {
            DB::beginTransaction();
            $file = DB::connection('aegis_tech')->table('files')->where('id', '=', $image->file_id)->get()->toArray();
            $product = Product::find($image->entity_id);
                $product->addMediaFromUrl('http://www.aegiztech.com/newsite/public/storage/'.$file[0]->path)
                    ->withResponsiveImages()
                    ->toMediaCollection('product');

            DB::commit();
        }

        return "All Images are transferred.";
    }

PHP Version: 7.4 Laravel Version: 7

0 likes
11 replies
Nakov's avatar

This is your problem $file[0]->path

Make sure you have file before you try to access its path:

$file = DB::connection('aegis_tech')->table('files')->where('id', '=', $image->file_id)->get()->toArray();

if (count($file))
{
    $product = Product::find($image->entity_id);
    $product->addMediaFromUrl('http://www.aegiztech.com/newsite/public/storage/'.$file[0]->path)
        ->withResponsiveImages()
        ->toMediaCollection('product');
}
Sinnbeck's avatar

You can get the first item just

DB::connection('aegis_tech')->table('files')->where('id', '=', $image->file_id)->first();

if ($file) {
    $product = Product::find($image->entity_id);
    $product->addMediaFromUrl('http://www.aegiztech.com/newsite/public/storage/'.$file->path)
        ->withResponsiveImages()
        ->toMediaCollection('product');
}
Sinnbeck's avatar

@sanjayacloud on what line? The code here shouldnt be able to cause that error as there is no array accessing happening

piljac1's avatar

@Sinnbeck you're right. It's most likely unrelated to the given code. Because even in his original code, I don't see how he could have gotten that error. "Undefined offset" would have been possible, but "Trying to access array offset on value of type null" not so much, because the only array access I can see in his code is on an array.

1 like
sanjayacloud's avatar

@Sinnbeck You guys are correct this error is not related to my code. Here is the screenshot of the code. https://prnt.sc/26iyksi I do not have any idea about where I am wrong. I am trying to upload images from URL using Spatie Laravel-medialibrary v8

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@sanjayacloud Sounds like you have set the queue driver to null. Check your .env file

QUEUE_CONNECTION=sync

If you are using another connection, then check queue.php that the connection specified has a driver

sanjayacloud's avatar

@Sinnbeck You have saved my time. It was as "QUEUE_CONNECTION=sync" in my .env file. Now code is working. Thank you so much.

Please or to participate in this conversation.