这是我的控制者:
$transaction = new ProductTransactionLog();
Event::fire(new SendResponseToProduct($response, $transaction));
这是事件类(SendResponseToProduct):
use Illuminate\Queue\SerializesModels;
use App\Models\ProductTransactionLog;
class SendResponseToProduct extends Event
{
use SerializesModels;
public $response = [];
public $transaction;
/**
* Create a new event instance.
*/
public function __construct($response = [], ProductTransactionLog $product_transaction)
{
// dd($transaction);
$this->response = $response;
$this->transaction = $product_transaction;
// dd($this->transaction);
}
}
这是在EventServiceProvider中:
protected $listen = [
'App\Events\SendResponseToProduct' => [
'App\Listeners\ResponseToProductListener',
],
];
当我运行控制器时,即当我触发事件时,我得到了这个奇怪的错误:
Handler.php第46行中的NotFoundHttpException:没有查询结果 model [App \ Models \ ProductTransactionLog]。
当我在Event构造函数中dd
时,我得到了对象。当我取消注释dd
时,我再次收到错误。
当我在Listener类中dd
时,我得到同样的错误。它没有到达Listener类。
我在这里失踪的是什么?
答案 0 :(得分:0)
您在SerializesModels
中使用SendResponseToProduct
特征作为队列(可能是同步)。它总是序列化和反序列化所有字段。
此特征特别检查天气序列化字段(在此示例中为:public $ transaction;)是ModelIdentifier
的实例(Model
对象的序列化字段是 - 因为Model
class是QueueableEntity
接口的实例)。如果是,那么他试图通过id
在数据库中查找模型的记录,在这种情况下,您不会使用此模型,因为您只是在没有任何数据的情况下创建它。
所以你可以做的是删除SerializesModels
特征,如果你没有使用队列 - 或者不在这里传递empyt模型。