Importer: beforesave() hook check

good day, i want to make the following code possible
protected function beforeSave(): void
{
// passing with the data a field that is for created by user
$this->data['created_by'] = Auth::id();
}
protected function beforeSave(): void
{
// passing with the data a field that is for created by user
$this->data['created_by'] = Auth::id();
}
is it correct? because i tried it and it didn't work. how do i make it better?
2 Replies
HeartlandTechie
HeartlandTechie7mo ago
Did you ever get a response on this one? I'm running into a similar problem. I'm wondering if it's because the importer is running as a service and doesn't have a auth()->user?
RoseRiyadh
RoseRiyadhOP7mo ago
@HeartlandTechie I resolved it like this in XImporter.php public function resolveRecord(): ?Voucher { $product = Product::where('slug', $this->data['slug'])->first(); if (!is_null($product)) { return new Voucher([ 'serial_voucher' => $this->data['serial_voucher'], 'product_id' => $product->id, 'created_by' => Auth::id(), ]); } return null; }
Want results from more Discord servers?
Add your server