Error with saved method running migration












1














In my laravel 5.7 app I use Elasticsearch and in model I use saved and deleted methods of the related Model :



<?php

namespace App;

use DB;
use AppMyAppModel;
use IlluminateSupportFacadesFile;
use IlluminateSupportFacadesStorage;

class Vote extends MyAppModel
{
protected $table = 'votes';
protected $elasticsearch_type = 'vote';
protected $primaryKey = 'id';
public $timestamps = false;

public function getTableName(): string
{
return $this->table;
}



protected static function boot() {
parent::boot();

static::deleted(function ($vote ) {
// return; // TO UNCOMMENT
$elastic = app(AppElasticElastic::class);
$elasticsearch_root_index = config('app.elasticsearch_root_index');
$elasticsearch_type = with(new Vote)->getElasticsearchType();
$elastic->delete([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
]);
});

static::saved(function ($vote) {
// return; // TO UNCOMMENT
$elastic = app(AppElasticElastic::class);
$elasticsearch_root_index = config('app.elasticsearch_root_index');
$elasticsearch_type = with(new Vote)->getElasticsearchType();

$elastic->delete([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
]);

if ($vote->status == 'A') { // only active votes must be saved in elasticsearch
$elastic->index([
'index' => $elasticsearch_root_index,
'type' => $elasticsearch_type,
'id' => $vote->id,
'body' => [
'id' => $vote->id,
'slug' => $vote->slug,
'name' => $vote->name,
'description' => $vote->description,
]
]);
} // if ($vote->status == 'A') { // only active votes must be saved in elasticsearch


When I modify my data in my editor it works ok, but running seeder command



php artisan migrate:refresh --seed -v


I got error :



...
Migrating: 2018_07_13_150151_create_votes_table

ElasticsearchCommonExceptionsMissing404Exception : {"_index":"select_vote","_type":"vote","_id":"2","_version":1,"result":"not_found","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":18,"_primary_term":23}

at /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:607
603| $exception = new AlreadyExpiredException($responseBody, $statusCode);
604| } elseif ($statusCode === 403) {
605| $exception = new Forbidden403Exception($responseBody, $statusCode);
606| } elseif ($statusCode === 404) {
> 607| $exception = new Missing404Exception($responseBody, $statusCode);
608| } elseif ($statusCode === 409) {
609| $exception = new Conflict409Exception($responseBody, $statusCode);
610| } elseif ($statusCode === 400 && strpos($responseBody, 'script_lang not supported') !== false) {
611| $exception = new ScriptLangNotSupportedException($responseBody. $statusCode);

Exception trace:

1 ElasticsearchConnectionsConnection::process4xxError()
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:279

2 ElasticsearchConnectionsConnection::ElasticsearchConnections{closure}()
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/react/promise/src/FulfilledPromise.php:25

3 ReactPromiseFulfilledPromise::then(Object(Closure))
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/guzzlehttp/ringphp/src/Future/CompletedFutureValue.php:55

4 GuzzleHttpRingFutureCompletedFutureValue::then(Object(Closure))
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/guzzlehttp/ringphp/src/Core.php:341

5 GuzzleHttpRingCore::proxy(Object(GuzzleHttpRingFutureCompletedFutureArray), Object(Closure))
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:299

6 ElasticsearchConnectionsConnection::ElasticsearchConnections{closure}(Object(ElasticsearchConnectionsConnection), Object(ElasticsearchTransport), )
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:177

7 ElasticsearchConnectionsConnection::performRequest("DELETE", "/select_vote/vote/2", , , Object(ElasticsearchTransport))
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Transport.php:110

8 ElasticsearchTransport::performRequest("DELETE", "/select_vote/vote/2", , )
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Client.php:1553

9 ElasticsearchClient::performRequest(Object(ElasticsearchEndpointsDelete))
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Client.php:265

10 ElasticsearchClient::delete()
/mnt/_work_sdb8/wwwroot/lar/Votes/app/Elastic.php:33

11 AppElasticElastic::delete(["select_vote", "vote"])
/mnt/_work_sdb8/wwwroot/lar/Votes/app/Vote.php:142

12 AppVote::App{closure}(Object(AppVote))
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:360

13 IlluminateEventsDispatcher::IlluminateEvents{closure}("eloquent.saved: AppVote")
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:209

14 IlluminateEventsDispatcher::dispatch("eloquent.saved: AppVote")
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php:162

15 IlluminateDatabaseEloquentModel::fireModelEvent("saved")
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:692

16 IlluminateDatabaseEloquentModel::finishSave()
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:663

17 IlluminateDatabaseEloquentModel::save()
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:790

18 IlluminateDatabaseEloquentBuilder::IlluminateDatabaseEloquent{closure}(Object(AppVote))
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Support/helpers.php:1027

19 tap(Object(AppVote), Object(Closure))
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:791

20 IlluminateDatabaseEloquentBuilder::create()
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:23

21 IlluminateDatabaseEloquentModel::forwardCallTo(Object(IlluminateDatabaseEloquentBuilder), "create")
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1608

22 IlluminateDatabaseEloquentModel::__call("create")
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1620

23 IlluminateDatabaseEloquentModel::__callStatic("create")
/mnt/_work_sdb8/wwwroot/lar/Votes/database/seeds/votesInitData.php:92

24 votesInitData::run()
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29

25 call_user_func_array()
/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29

...


If in saved and deleted methods to uncomment return that it works ok (without coping to elasticsearch).
Why Error and how to fix it?
Is there is a way to check that it is called from migration command and exit? Could this salve the problem ?



Thanks!










share|improve this question



























    1














    In my laravel 5.7 app I use Elasticsearch and in model I use saved and deleted methods of the related Model :



    <?php

    namespace App;

    use DB;
    use AppMyAppModel;
    use IlluminateSupportFacadesFile;
    use IlluminateSupportFacadesStorage;

    class Vote extends MyAppModel
    {
    protected $table = 'votes';
    protected $elasticsearch_type = 'vote';
    protected $primaryKey = 'id';
    public $timestamps = false;

    public function getTableName(): string
    {
    return $this->table;
    }



    protected static function boot() {
    parent::boot();

    static::deleted(function ($vote ) {
    // return; // TO UNCOMMENT
    $elastic = app(AppElasticElastic::class);
    $elasticsearch_root_index = config('app.elasticsearch_root_index');
    $elasticsearch_type = with(new Vote)->getElasticsearchType();
    $elastic->delete([
    'index' => $elasticsearch_root_index,
    'type' => $elasticsearch_type,
    'id' => $vote->id,
    ]);
    });

    static::saved(function ($vote) {
    // return; // TO UNCOMMENT
    $elastic = app(AppElasticElastic::class);
    $elasticsearch_root_index = config('app.elasticsearch_root_index');
    $elasticsearch_type = with(new Vote)->getElasticsearchType();

    $elastic->delete([
    'index' => $elasticsearch_root_index,
    'type' => $elasticsearch_type,
    'id' => $vote->id,
    ]);

    if ($vote->status == 'A') { // only active votes must be saved in elasticsearch
    $elastic->index([
    'index' => $elasticsearch_root_index,
    'type' => $elasticsearch_type,
    'id' => $vote->id,
    'body' => [
    'id' => $vote->id,
    'slug' => $vote->slug,
    'name' => $vote->name,
    'description' => $vote->description,
    ]
    ]);
    } // if ($vote->status == 'A') { // only active votes must be saved in elasticsearch


    When I modify my data in my editor it works ok, but running seeder command



    php artisan migrate:refresh --seed -v


    I got error :



    ...
    Migrating: 2018_07_13_150151_create_votes_table

    ElasticsearchCommonExceptionsMissing404Exception : {"_index":"select_vote","_type":"vote","_id":"2","_version":1,"result":"not_found","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":18,"_primary_term":23}

    at /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:607
    603| $exception = new AlreadyExpiredException($responseBody, $statusCode);
    604| } elseif ($statusCode === 403) {
    605| $exception = new Forbidden403Exception($responseBody, $statusCode);
    606| } elseif ($statusCode === 404) {
    > 607| $exception = new Missing404Exception($responseBody, $statusCode);
    608| } elseif ($statusCode === 409) {
    609| $exception = new Conflict409Exception($responseBody, $statusCode);
    610| } elseif ($statusCode === 400 && strpos($responseBody, 'script_lang not supported') !== false) {
    611| $exception = new ScriptLangNotSupportedException($responseBody. $statusCode);

    Exception trace:

    1 ElasticsearchConnectionsConnection::process4xxError()
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:279

    2 ElasticsearchConnectionsConnection::ElasticsearchConnections{closure}()
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/react/promise/src/FulfilledPromise.php:25

    3 ReactPromiseFulfilledPromise::then(Object(Closure))
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/guzzlehttp/ringphp/src/Future/CompletedFutureValue.php:55

    4 GuzzleHttpRingFutureCompletedFutureValue::then(Object(Closure))
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/guzzlehttp/ringphp/src/Core.php:341

    5 GuzzleHttpRingCore::proxy(Object(GuzzleHttpRingFutureCompletedFutureArray), Object(Closure))
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:299

    6 ElasticsearchConnectionsConnection::ElasticsearchConnections{closure}(Object(ElasticsearchConnectionsConnection), Object(ElasticsearchTransport), )
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:177

    7 ElasticsearchConnectionsConnection::performRequest("DELETE", "/select_vote/vote/2", , , Object(ElasticsearchTransport))
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Transport.php:110

    8 ElasticsearchTransport::performRequest("DELETE", "/select_vote/vote/2", , )
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Client.php:1553

    9 ElasticsearchClient::performRequest(Object(ElasticsearchEndpointsDelete))
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Client.php:265

    10 ElasticsearchClient::delete()
    /mnt/_work_sdb8/wwwroot/lar/Votes/app/Elastic.php:33

    11 AppElasticElastic::delete(["select_vote", "vote"])
    /mnt/_work_sdb8/wwwroot/lar/Votes/app/Vote.php:142

    12 AppVote::App{closure}(Object(AppVote))
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:360

    13 IlluminateEventsDispatcher::IlluminateEvents{closure}("eloquent.saved: AppVote")
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:209

    14 IlluminateEventsDispatcher::dispatch("eloquent.saved: AppVote")
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php:162

    15 IlluminateDatabaseEloquentModel::fireModelEvent("saved")
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:692

    16 IlluminateDatabaseEloquentModel::finishSave()
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:663

    17 IlluminateDatabaseEloquentModel::save()
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:790

    18 IlluminateDatabaseEloquentBuilder::IlluminateDatabaseEloquent{closure}(Object(AppVote))
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Support/helpers.php:1027

    19 tap(Object(AppVote), Object(Closure))
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:791

    20 IlluminateDatabaseEloquentBuilder::create()
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:23

    21 IlluminateDatabaseEloquentModel::forwardCallTo(Object(IlluminateDatabaseEloquentBuilder), "create")
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1608

    22 IlluminateDatabaseEloquentModel::__call("create")
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1620

    23 IlluminateDatabaseEloquentModel::__callStatic("create")
    /mnt/_work_sdb8/wwwroot/lar/Votes/database/seeds/votesInitData.php:92

    24 votesInitData::run()
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29

    25 call_user_func_array()
    /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29

    ...


    If in saved and deleted methods to uncomment return that it works ok (without coping to elasticsearch).
    Why Error and how to fix it?
    Is there is a way to check that it is called from migration command and exit? Could this salve the problem ?



    Thanks!










    share|improve this question

























      1












      1








      1







      In my laravel 5.7 app I use Elasticsearch and in model I use saved and deleted methods of the related Model :



      <?php

      namespace App;

      use DB;
      use AppMyAppModel;
      use IlluminateSupportFacadesFile;
      use IlluminateSupportFacadesStorage;

      class Vote extends MyAppModel
      {
      protected $table = 'votes';
      protected $elasticsearch_type = 'vote';
      protected $primaryKey = 'id';
      public $timestamps = false;

      public function getTableName(): string
      {
      return $this->table;
      }



      protected static function boot() {
      parent::boot();

      static::deleted(function ($vote ) {
      // return; // TO UNCOMMENT
      $elastic = app(AppElasticElastic::class);
      $elasticsearch_root_index = config('app.elasticsearch_root_index');
      $elasticsearch_type = with(new Vote)->getElasticsearchType();
      $elastic->delete([
      'index' => $elasticsearch_root_index,
      'type' => $elasticsearch_type,
      'id' => $vote->id,
      ]);
      });

      static::saved(function ($vote) {
      // return; // TO UNCOMMENT
      $elastic = app(AppElasticElastic::class);
      $elasticsearch_root_index = config('app.elasticsearch_root_index');
      $elasticsearch_type = with(new Vote)->getElasticsearchType();

      $elastic->delete([
      'index' => $elasticsearch_root_index,
      'type' => $elasticsearch_type,
      'id' => $vote->id,
      ]);

      if ($vote->status == 'A') { // only active votes must be saved in elasticsearch
      $elastic->index([
      'index' => $elasticsearch_root_index,
      'type' => $elasticsearch_type,
      'id' => $vote->id,
      'body' => [
      'id' => $vote->id,
      'slug' => $vote->slug,
      'name' => $vote->name,
      'description' => $vote->description,
      ]
      ]);
      } // if ($vote->status == 'A') { // only active votes must be saved in elasticsearch


      When I modify my data in my editor it works ok, but running seeder command



      php artisan migrate:refresh --seed -v


      I got error :



      ...
      Migrating: 2018_07_13_150151_create_votes_table

      ElasticsearchCommonExceptionsMissing404Exception : {"_index":"select_vote","_type":"vote","_id":"2","_version":1,"result":"not_found","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":18,"_primary_term":23}

      at /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:607
      603| $exception = new AlreadyExpiredException($responseBody, $statusCode);
      604| } elseif ($statusCode === 403) {
      605| $exception = new Forbidden403Exception($responseBody, $statusCode);
      606| } elseif ($statusCode === 404) {
      > 607| $exception = new Missing404Exception($responseBody, $statusCode);
      608| } elseif ($statusCode === 409) {
      609| $exception = new Conflict409Exception($responseBody, $statusCode);
      610| } elseif ($statusCode === 400 && strpos($responseBody, 'script_lang not supported') !== false) {
      611| $exception = new ScriptLangNotSupportedException($responseBody. $statusCode);

      Exception trace:

      1 ElasticsearchConnectionsConnection::process4xxError()
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:279

      2 ElasticsearchConnectionsConnection::ElasticsearchConnections{closure}()
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/react/promise/src/FulfilledPromise.php:25

      3 ReactPromiseFulfilledPromise::then(Object(Closure))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/guzzlehttp/ringphp/src/Future/CompletedFutureValue.php:55

      4 GuzzleHttpRingFutureCompletedFutureValue::then(Object(Closure))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/guzzlehttp/ringphp/src/Core.php:341

      5 GuzzleHttpRingCore::proxy(Object(GuzzleHttpRingFutureCompletedFutureArray), Object(Closure))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:299

      6 ElasticsearchConnectionsConnection::ElasticsearchConnections{closure}(Object(ElasticsearchConnectionsConnection), Object(ElasticsearchTransport), )
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:177

      7 ElasticsearchConnectionsConnection::performRequest("DELETE", "/select_vote/vote/2", , , Object(ElasticsearchTransport))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Transport.php:110

      8 ElasticsearchTransport::performRequest("DELETE", "/select_vote/vote/2", , )
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Client.php:1553

      9 ElasticsearchClient::performRequest(Object(ElasticsearchEndpointsDelete))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Client.php:265

      10 ElasticsearchClient::delete()
      /mnt/_work_sdb8/wwwroot/lar/Votes/app/Elastic.php:33

      11 AppElasticElastic::delete(["select_vote", "vote"])
      /mnt/_work_sdb8/wwwroot/lar/Votes/app/Vote.php:142

      12 AppVote::App{closure}(Object(AppVote))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:360

      13 IlluminateEventsDispatcher::IlluminateEvents{closure}("eloquent.saved: AppVote")
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:209

      14 IlluminateEventsDispatcher::dispatch("eloquent.saved: AppVote")
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php:162

      15 IlluminateDatabaseEloquentModel::fireModelEvent("saved")
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:692

      16 IlluminateDatabaseEloquentModel::finishSave()
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:663

      17 IlluminateDatabaseEloquentModel::save()
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:790

      18 IlluminateDatabaseEloquentBuilder::IlluminateDatabaseEloquent{closure}(Object(AppVote))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Support/helpers.php:1027

      19 tap(Object(AppVote), Object(Closure))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:791

      20 IlluminateDatabaseEloquentBuilder::create()
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:23

      21 IlluminateDatabaseEloquentModel::forwardCallTo(Object(IlluminateDatabaseEloquentBuilder), "create")
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1608

      22 IlluminateDatabaseEloquentModel::__call("create")
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1620

      23 IlluminateDatabaseEloquentModel::__callStatic("create")
      /mnt/_work_sdb8/wwwroot/lar/Votes/database/seeds/votesInitData.php:92

      24 votesInitData::run()
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29

      25 call_user_func_array()
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29

      ...


      If in saved and deleted methods to uncomment return that it works ok (without coping to elasticsearch).
      Why Error and how to fix it?
      Is there is a way to check that it is called from migration command and exit? Could this salve the problem ?



      Thanks!










      share|improve this question













      In my laravel 5.7 app I use Elasticsearch and in model I use saved and deleted methods of the related Model :



      <?php

      namespace App;

      use DB;
      use AppMyAppModel;
      use IlluminateSupportFacadesFile;
      use IlluminateSupportFacadesStorage;

      class Vote extends MyAppModel
      {
      protected $table = 'votes';
      protected $elasticsearch_type = 'vote';
      protected $primaryKey = 'id';
      public $timestamps = false;

      public function getTableName(): string
      {
      return $this->table;
      }



      protected static function boot() {
      parent::boot();

      static::deleted(function ($vote ) {
      // return; // TO UNCOMMENT
      $elastic = app(AppElasticElastic::class);
      $elasticsearch_root_index = config('app.elasticsearch_root_index');
      $elasticsearch_type = with(new Vote)->getElasticsearchType();
      $elastic->delete([
      'index' => $elasticsearch_root_index,
      'type' => $elasticsearch_type,
      'id' => $vote->id,
      ]);
      });

      static::saved(function ($vote) {
      // return; // TO UNCOMMENT
      $elastic = app(AppElasticElastic::class);
      $elasticsearch_root_index = config('app.elasticsearch_root_index');
      $elasticsearch_type = with(new Vote)->getElasticsearchType();

      $elastic->delete([
      'index' => $elasticsearch_root_index,
      'type' => $elasticsearch_type,
      'id' => $vote->id,
      ]);

      if ($vote->status == 'A') { // only active votes must be saved in elasticsearch
      $elastic->index([
      'index' => $elasticsearch_root_index,
      'type' => $elasticsearch_type,
      'id' => $vote->id,
      'body' => [
      'id' => $vote->id,
      'slug' => $vote->slug,
      'name' => $vote->name,
      'description' => $vote->description,
      ]
      ]);
      } // if ($vote->status == 'A') { // only active votes must be saved in elasticsearch


      When I modify my data in my editor it works ok, but running seeder command



      php artisan migrate:refresh --seed -v


      I got error :



      ...
      Migrating: 2018_07_13_150151_create_votes_table

      ElasticsearchCommonExceptionsMissing404Exception : {"_index":"select_vote","_type":"vote","_id":"2","_version":1,"result":"not_found","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":18,"_primary_term":23}

      at /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:607
      603| $exception = new AlreadyExpiredException($responseBody, $statusCode);
      604| } elseif ($statusCode === 403) {
      605| $exception = new Forbidden403Exception($responseBody, $statusCode);
      606| } elseif ($statusCode === 404) {
      > 607| $exception = new Missing404Exception($responseBody, $statusCode);
      608| } elseif ($statusCode === 409) {
      609| $exception = new Conflict409Exception($responseBody, $statusCode);
      610| } elseif ($statusCode === 400 && strpos($responseBody, 'script_lang not supported') !== false) {
      611| $exception = new ScriptLangNotSupportedException($responseBody. $statusCode);

      Exception trace:

      1 ElasticsearchConnectionsConnection::process4xxError()
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:279

      2 ElasticsearchConnectionsConnection::ElasticsearchConnections{closure}()
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/react/promise/src/FulfilledPromise.php:25

      3 ReactPromiseFulfilledPromise::then(Object(Closure))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/guzzlehttp/ringphp/src/Future/CompletedFutureValue.php:55

      4 GuzzleHttpRingFutureCompletedFutureValue::then(Object(Closure))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/guzzlehttp/ringphp/src/Core.php:341

      5 GuzzleHttpRingCore::proxy(Object(GuzzleHttpRingFutureCompletedFutureArray), Object(Closure))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:299

      6 ElasticsearchConnectionsConnection::ElasticsearchConnections{closure}(Object(ElasticsearchConnectionsConnection), Object(ElasticsearchTransport), )
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:177

      7 ElasticsearchConnectionsConnection::performRequest("DELETE", "/select_vote/vote/2", , , Object(ElasticsearchTransport))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Transport.php:110

      8 ElasticsearchTransport::performRequest("DELETE", "/select_vote/vote/2", , )
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Client.php:1553

      9 ElasticsearchClient::performRequest(Object(ElasticsearchEndpointsDelete))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Client.php:265

      10 ElasticsearchClient::delete()
      /mnt/_work_sdb8/wwwroot/lar/Votes/app/Elastic.php:33

      11 AppElasticElastic::delete(["select_vote", "vote"])
      /mnt/_work_sdb8/wwwroot/lar/Votes/app/Vote.php:142

      12 AppVote::App{closure}(Object(AppVote))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:360

      13 IlluminateEventsDispatcher::IlluminateEvents{closure}("eloquent.saved: AppVote")
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:209

      14 IlluminateEventsDispatcher::dispatch("eloquent.saved: AppVote")
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php:162

      15 IlluminateDatabaseEloquentModel::fireModelEvent("saved")
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:692

      16 IlluminateDatabaseEloquentModel::finishSave()
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:663

      17 IlluminateDatabaseEloquentModel::save()
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:790

      18 IlluminateDatabaseEloquentBuilder::IlluminateDatabaseEloquent{closure}(Object(AppVote))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Support/helpers.php:1027

      19 tap(Object(AppVote), Object(Closure))
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:791

      20 IlluminateDatabaseEloquentBuilder::create()
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:23

      21 IlluminateDatabaseEloquentModel::forwardCallTo(Object(IlluminateDatabaseEloquentBuilder), "create")
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1608

      22 IlluminateDatabaseEloquentModel::__call("create")
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1620

      23 IlluminateDatabaseEloquentModel::__callStatic("create")
      /mnt/_work_sdb8/wwwroot/lar/Votes/database/seeds/votesInitData.php:92

      24 votesInitData::run()
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29

      25 call_user_func_array()
      /mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29

      ...


      If in saved and deleted methods to uncomment return that it works ok (without coping to elasticsearch).
      Why Error and how to fix it?
      Is there is a way to check that it is called from migration command and exit? Could this salve the problem ?



      Thanks!







      elasticsearch laravel-5 eloquent






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 18 '18 at 8:19







      user2054381































          1 Answer
          1






          active

          oldest

          votes


















          0














          In similar case I set flag in my settings table in the beginning of Migrations scripts, like



          DB::table('settings')->insert([
          'name' => 'elastic_automation',
          'value' => 'N',
          ]);


          In the last migration script I change the key



          Settings::where('name', 'elastic_automation')->update(['value' => 'Y']);


          and I check this key in my model events. That works for me!






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359042%2ferror-with-saved-method-running-migration%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown
























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            In similar case I set flag in my settings table in the beginning of Migrations scripts, like



            DB::table('settings')->insert([
            'name' => 'elastic_automation',
            'value' => 'N',
            ]);


            In the last migration script I change the key



            Settings::where('name', 'elastic_automation')->update(['value' => 'Y']);


            and I check this key in my model events. That works for me!






            share|improve this answer


























              0














              In similar case I set flag in my settings table in the beginning of Migrations scripts, like



              DB::table('settings')->insert([
              'name' => 'elastic_automation',
              'value' => 'N',
              ]);


              In the last migration script I change the key



              Settings::where('name', 'elastic_automation')->update(['value' => 'Y']);


              and I check this key in my model events. That works for me!






              share|improve this answer
























                0












                0








                0






                In similar case I set flag in my settings table in the beginning of Migrations scripts, like



                DB::table('settings')->insert([
                'name' => 'elastic_automation',
                'value' => 'N',
                ]);


                In the last migration script I change the key



                Settings::where('name', 'elastic_automation')->update(['value' => 'Y']);


                and I check this key in my model events. That works for me!






                share|improve this answer












                In similar case I set flag in my settings table in the beginning of Migrations scripts, like



                DB::table('settings')->insert([
                'name' => 'elastic_automation',
                'value' => 'N',
                ]);


                In the last migration script I change the key



                Settings::where('name', 'elastic_automation')->update(['value' => 'Y']);


                and I check this key in my model events. That works for me!







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 13:32









                mstdmstdmstdmstd

                117514




                117514






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53359042%2ferror-with-saved-method-running-migration%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to change which sound is reproduced for terminal bell?

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?