Laravel事件未在控制器中触发

时间:2018-06-08 11:02:25

标签: laravel events laravel-5.4 laravel-5.5 laravel-events

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

    }
}

1 个答案:

答案 0 :(得分:1)

确保注册您的监听器以在EventServiceProvider

中收听它的相应事件

转到App \ Providers \ EventServiceProvider,您会发现名为listen

的受保护数组
protected $listen = [

] 

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

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

希望能解决问题。