1

I try to send notification via pusher when user submitted new order.

in my CartController where I save data into my orders table I have:

<?php

use App\Events\UserOrdered;

//codes....
        public function checkout(Request $request) {

        //rest of the code

        Auth::user()->orders()->save($order);

        event(new UserOrdered($order));

        //Cart::clear();
        Session::flash('success', 'Thank you. Your order has been received.');
        return redirect()->route('ordersindex');
    }

when I hit to save my order i will get 404 NOT FOUND on event(new UserOrdered($order));

Here is my Event code:

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Order;

class UserOrdered implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $order;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('order-placed');
    }
}

This is my Listener code:

<?php

namespace App\Listeners;

use App\Events\UserOrdered;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class SendNotification
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  UserOrdered  $event
     * @return void
     */
    public function handle(UserOrdered $event)
    {
        $pusher->trigger($event, 'order-placed', $order);
    }
}

PS: When I try my pusher from pusher website Debug Console i can get notification. Whatever problem is, is from event and listener.

Any idea?

UPDATE

screen1

update 2

I managed to get pusher to work (i know is not the best way but it works at least).

here is my listener now:

<?php

namespace App\Listeners;

use App\Events\UserOrdered;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Pusher\Pusher;
use Mail;


class SendNotification
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  UserOrdered  $event
     * @return void
     */
    public function handle(UserOrdered $event)
    {
        Mail::to($event->order->buyer_email)->send(new UserOrdered($event->order));

        $options = array(
            'cluster' => 'ap1', 
            'encrypted' => true
        );
        $pusher = new Pusher(
            'xxxxx', //id
            'xxxxx', //key
            'xxxx', //secret_key
            $options
        );
        $pusher->trigger('UserOrdered', 'order-placed', $event->order);
    }
}

What I need?

  1. Pusher will show notification in admin as new order placed. done
  2. I also want to send 2 emails one to admin to say he/she has new order, and another to user to say that we received his/her order.

mail function:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Order;

class UserOrdered extends Mailable
{
    use Queueable, SerializesModels;
    public $order;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from('noreply@tester.com')->subject('Your Order recieved!')->markdown('emails.orders');
    }
}

Progress: In order to send emails I added Mail::to in my listener as you see above, and added code below to my controller function:

//rest of the code...
      Auth::user()->orders()->save($order);

      $user = $order->buyer_email;

      event(new UserOrdered($order));
      Mail::to($request->user)->send(new UserOrdered($order)); //added newly 

      //Cart::clear();
      Session::flash('success', 'Thank you. Your order has been received.');
      return redirect()->route('ordersindex');

Problem is:

I get this error

Type error: Argument 1 passed to Illuminate\Mail\PendingMail::send() must be an instance of Illuminate\Mail\Mailable, instance of App\Events\UserOrdered given

refers to: Mail::to($event->order->buyer_email)->send(new UserOrdered($event->order));

if I change $event->order at the end of that to something static like $event I will get:

Type error: Argument 1 passed to App\Events\UserOrdered::__construct() must be an instance of App\Order, instance of App\Events\UserOrdered given

any idea?

mafortis
  • 6,750
  • 23
  • 130
  • 288
  • I think you forgot to define `$pusher` and `$order` variables in `SendNotification@handle`. – Camilo Jan 31 '18 at 05:19
  • I tried this `pusher::trigger($event, 'order-placed', $event->order);` and also added `use Pusher\Pusher;` to replace `$pusher` but still error comes from controller and not listener. – mafortis Jan 31 '18 at 05:22
  • @Camilo this error i have `throw new BroadcastException( is_bool($response) ? 'Failed to connect to Pusher.' : $response['body'] );` from `Illuminate\Broadcasting\Broadcasters\PusherBroadcaster.php` – mafortis Jan 31 '18 at 05:24
  • The error is not from your controller, it's a `BroadcastException`. Check the Pusher configuration in your Laravel app. – Camilo Jan 31 '18 at 05:28
  • @Camilo As I said my pusher works fine if send notification from pusher website. I don't think problem is because my configration. I think is about how to introduce it to my listener. do you have any idea how can i do that? – mafortis Jan 31 '18 at 05:29
  • Of course using the Pusher website is going to work. The problem comes from your Laravel app not being able to reach the Pusher API. – Camilo Jan 31 '18 at 05:31
  • 3
    So you've named both your event and your mailable `UserOrdered`? Are you `use`ing the correct one when you go to send the mail? Have you `composer dumpautoload`ed in case of caching problems? –  Jan 31 '18 at 06:54
  • @btl yes the names are the same because later will be many more, just to avoid confusing. no i didnt try dumpautoload but i will. – mafortis Jan 31 '18 at 06:56
  • 2
    Yes @btl he used the wrong `UserOrdered`. @mafortis your `new UserOrdered($event->order)`, the class here is referring to the event, not the mailable. Either in the `use` statement you rename the class (`as`), or you use full qualified namespace, as `new \App\Mail\UserOrdered($event->order)` – Lionel Chan Jan 31 '18 at 07:03

0 Answers0