0

我在 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]);

    }
}
4

1 回答 1

1

确保注册您的侦听器以在 EventServiceProvider 中侦听它的相应事件

转到 App\Providers\EventServiceProvider 你会发现名为listen的受保护数组

protected $listen = [

] 

在您的情况下,您的活动可以如下所示进行注册

protected $listen = [
    'App\Events\CalculateCustomerBookingolCharges' => [
        'App\Listeners\CustomerBookingolCharges',
    ],
];

希望能解决问题。

于 2018-06-08T12:07:49.343 回答