我几天来一直坚持这个问题:
我有一个服务提供商,负责监控以下Model事件:
DeliveryOrder::created(function (DeliveryOrder $order) {
$user = Sentinel::getUser();
Activity::create([
'company_id' => $order->company_id,
'user_id' => $user->getUserId(),
'label_type' => 'success',
'icon' => 'fa fa-map-marker',
'description' => "{$user->first_name} {$user->last_name} created a new <a href='{$order->getShowLink()}'>{$order->delivery_type}-type delivery order</a> Delivery OR number <a href='{$order->getShowLink()}'>{$order->delivery_or_no}</a>",
]);
Movement::create([
'company_id' => $order->company_id,
'delivery_order_id' => $order->id,
'label_type' => 'success',
]);
});
DeliveryOrder::updated(function (DeliveryOrder $order) {
Log::info($order);
$movement = Movement::where('delivery_order_id', $order->id)->first();
$movement->icon = ($order->getType() == 'incoming') ? 'fa fa-reply' : 'fa fa-share';
$user = $order->user->getFullName();
$operation = ($order->getType() == 'incoming') ? 'Incoming' : 'Outgoing';
$items = $order->items()->where('isAdjustment', false)->get()->groupBy('attribute_values');
$countedItems = $items->map(function ($item, $key) {
$item->put('count', $item->count());
return $item;
});
$content = "";
foreach ($countedItems as $item) {
$attributes = ($item[0]->attribute_values == "0") ? 'No Attributes' : $item[0]->attribute_values;
$text = $item[0]->product->ItemCode . ' : ' . $item[0]->product->parent->ItemName . ' ' . $attributes . ' for ' . $item["count"];
$content .= "<div class='desc'>{$text}</div>";
}
$movement->description = "{$user} created an {$operation} order for the following: {$content}";
$movement->save();
// dd($movement);
});
Adjustment::created(function (Adjustment $adjustment) {
Movement::create([
'company_id' => $adjustment->company_id,
'adjustment_id' => $adjustment->id,
'label_type' => 'info',
]);
});
Adjustment::updated(function (Adjustment $adjustment) {
Log::info($adjustment);
$attributes = ($adjustment->items[0]->attribute_values == "0") ? 'No Attributes' : $adjustment->items[0]->attribute_values;
$item = $adjustment->items[0]->product->ItemCode . ' : ' . $adjustment->items[0]->product->parent->ItemName . ' '
.$attributes;
$encoder = $adjustment->user->getFullName();
$count = $adjustment->items->count();
if ($adjustment->override()->count() > 0) {
$override_movement = Movement::where('override_id', $adjustment->override->id)->first();
$approver = $adjustment->override->user->getFullName();
$override_movement->description = "{$approver} approved {$encoder}\'s request for negative inventory adjustment for {$item} by {$count} ";
$override_movement->save();
}
$operation = (($adjustment->items[0]->type == '+') ? 'Added' : 'Reduced');
$movement = Movement::where('adjustment_id', $adjustment->id)->first();
$movement->icon = ($adjustment->items[0]->type == '+') ? 'fa fa-plus' : 'fa fa-minus';
$movement->description = "{$encoder} Adjusted a product: {$operation} {$item} by {$count} ";
$movement->save();
// dd("hehe");
});
Override::created(function (Override $override) {
Movement::create([
'company_id' => $override->company_id,
'override_id' => $override->id,
'label_type' => 'danger',
'icon' => 'fa fa-exclamation',
]);
});
~~~
创建的事件触发,因此我能够创建一个Movement实例,但更新的事件经常没有,这是非常令人头疼的事。我通过Model-&gt; touch()触发事件,因为我只是将关系添加到创建的模型中,如下所示:
$delivery_order = DeliveryOrder::create($data);
foreach($items as $item)
{
//some code creating an item here
$delivery_order->items()->save($item);
}
$delivery_order->touch();
return redirect()->action('DeliveryOrdersController@show',$delivery_order->id)->with('status','success!');
之前我怀疑是因为我使用redirect() - &gt; back()(我有使用它的理由) 但即使我使用redirect() - &gt; action(),它仍然无法正常工作。
有关我应该如何处理的任何建议?