mardi 19 mai 2020

Laravel Model::firstOrCreate(args) doesn't seem for be working as expected

I am importing a collection of data from a CSV file (I've created a DB seeder & Job class to handle that functionality). But I want to handle any potential duplicate entries. For that, I am using Model::firstOrCreate()

The following snippet, when executed with the Job class App\Jobs\ImportPosts, within the handle() method doesn't not work; instead it creates duplicate entries

  MyModel::firstOrCreate(
    [
      'foo' => $newPostData['foo'],
      'bar' => $newPostData['bar'],
    ], $newPostData);

On the other have, if I place the very same snippet in a Controller and execute it, it works as enticipated (portential duplicates are not created).

I'm I missing something here?


PS: For queueing, I am using predis thus App\Jobs\ImportPosts's hundle() looks something like this:

     Redis::throttle('fooBar')
        ->allow(5)
        ->every(30)
        ->then(function() {

          ....

          foreach($fileData as $newPostData) {

             ....

             MyModel::firstOrCreate(
                [
                  'foo' => $newPostData['foo'],
                  'bar' => $newPostData['bar'],
                ], $newPostData
             );

          }

        }, function() {

          return $this->release(30);

        });

Debug information:

The response(s) are: -

When executing within a Job: enter image description here

When executing in a Controller: enter image description here

So, the IF EXISTS check if happening, but the record is still being created. Any workaround on this?

Using: Laravel 5.5



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire