0

I Create Transport Booking System in Laravel. When Customer Book a New Trip to Transport i calculate Commission Amount using Event (CalculateCustomerBookingolCharges).

I try to book a new trip, all data's has been save but Commission Amount Not Calculated (Event Not Triggered).

Here is my Controller Code :

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)

}

Event Listeners :

<?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]);

    }
}
3
  • could you please show the CalculateCustomerBookingolCharges event/notification file? Commented Jun 8, 2018 at 11:30
  • Make sure you have configured the broadcasting setting in app.php and the event is implemented with ShouldBroadcast. Commented Jun 8, 2018 at 11:31
  • A random side note until you give more code. Wouldn't it be better to call the event "CustomerBookingolCharges" and the Listener "CalculateCu...."? Commented Jun 8, 2018 at 12:30

1 Answer 1

1

Make sure to register your listener to listen to it's corresponding event in the EventServiceProvider

Go to App\Providers\EventServiceProvider you will find protected array called listen

protected $listen = [

] 

In your case your event could be registered as shown below

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

Hope that solves the problem.

Sign up to request clarification or add additional context in comments.

1 Comment

can you edit the post and add a screen shot of your event class and your event service provider class ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.