我在 Laravel 中创建了交通预订系统。当客户预订新的运输旅行时,我使用事件 (CalculateCustomerBookingolCharges) 计算佣金金额。
我尝试预订新行程,所有数据都已保存,但未计算佣金金额(未触发事件)。
这是我的控制器代码:
public function savenewbooking (Request $request)
{
$createbooking=Booking::create([
'order_id' => $orderid,
'token' => $request->input('_token'),
'booking_id' => $curbookingid,
'invoice_prefix' => $bookingpre,
'userid' => $this->userid,
'user_email' => $customerData->email,
'trip_amt' => $tripamt,
'extra_amt' => $totalextraserviceamount,
'total_amt' => $totalbookingamount,
'ip_address' => $ipaddress,
'booking_status' => 1,
'created_by' => $this->userid
]);
event(new CalculateCustomerBookingolCharges($createbooking));
//Event::fire(new CalculateCustomerBookingolCharges($createbooking));
return response()->json([
'items' => $orderid,
'error '=> [
'code' => 'REQUEST_SUCCESSFUL',
'description'=>'Booking Confirmed'
]
],200)
}
事件监听器:
<?php
namespace App\Listeners;
use App\Events\CalculateCustomerBookingolCharges;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Booking;
use App\Appsettings;
use Auth;
class CustomerBookingolCharges
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param CalculateCustomerBookingolCharges $event
* @return void
*/
public function handle(CalculateCustomerBookingolCharges $event)
{
$ourlorrypercentage=Appsettings::Where('config_title','ourlorry_comission')->pluck('config_modified')->first();
$ourlorryamt=($ourlorrypercentage/100)*$event->booking->total_amt;
$updatebooking=Booking::Where('id',$event->booking->id)->update(['ourlorry_percentage'=>$ourlorrypercentage,'ourlorry_amount'=>$ourlorryamt,'updated_by'=>Auth::user()->id]);
}
}