lundi 31 janvier 2022

Laravel Dispatch method called from static method results in different Job Payload

I have refactored some code so that 2 public function created within a Controller file, have been moved into a Model file and as public static.

The following method is called from within a Controller:

public static function moveItem(Transactin $transaction)

and self::confirmPayment($order) is called within this method which calls:

private static function confirmPayment(Order $order)

Within this method a mailable is created and dispatched via SendPaymentConfirmation::dispatch($mailable, $order)

The construct of the Job Class is as follows:

public function __construct(OrderPaid $mailable, Order $order)
{
    $this->mailable = $mailable;
    $this->order = $order;
}

The code was working fine before moving it to static methods. This is from the working jobs.payload:

O:45:\"Illuminate\\Contracts\\Database\\ModelIdentifier\":4:{
  s:5:\"class\";
  s:9:\"App\\Order\";
  s:2:\"id\";i:123456;
  s:9:\"relations\";a:5:{i:0;
  s:8:\"payments\";i:1;
  s:21:\"payments.transactions\";i:2;
  s:14:\"customisations\";i:3;
  s:8:\"user\";i:4;
  s:7:\"creator\";}

Different payload that is saved after calling via the static methods:

O:45:\"Illuminate\\Contracts\\Database\\ModelIdentifier\":4:{
  s:5:\"class\";
  s:9:\"App\\Order\";
  s:2:\"id\";i:123456;
  s:9:\"relations\";a:11:{i:0;
  s:8:\"payments\";i:1;
  s:21:\"payments.transactions\";i:2;
  s:5:\"items\";i:3;
  s:19:\"items.customisation\";i:5;
  s:14:\"customisations\";i:8;
  s:8:\"user\";i:10;
  s:7:\"creator\";}

As you can see, it has added a number of additional relations. These are valid but strange that it is adding more. When the queue is run, Laravel returns the following error:

local.ERROR: Call to a member function customisation() on null
[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Call to a member function customisation() on null at /home/vagrant/code/test/app/Models/Order/Item.php:44)

I changed the constuct of the Job to use withoutRelations but still getting the error:

public function __construct(OrderPaid $mailable, Order $order)
{
    $this->mailable = $mailable;
    $this->order = $order->withoutRelations();
}

Is there a reason why the relations would be different when called via non-static vs static method?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire