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);

        });

Using: Laravel 5.5



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2WJcAZc
via IFTTT

Aucun commentaire:

Enregistrer un commentaire