Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

php - How to solve Call to undefined method IlluminateDatabaseQueryBuilder::notify()? (laravel 5.3)

My function create in controller is like this :

public function create()
{ 
    // This is dummpy data to testing checkout order event
    $data = array(
                'number'            => '2', 
                'user'              => 'chelsea',
                'store'             => 'chelsea shop', 
                'total_amount'      => 11000000, 
                'total_product'     => 2,
                'status'            => '92000000', 
                'delivery_address'  => 'london',
                'email'             => '[email protected]'
            );
    $data = Order::where('number', $data['number'])->first();
    Event::fire(new CheckoutOrderEvent($data));
}

My CheckoutOrderListener is like this :

<?php
namespace AppListeners;
use AppEventsCheckoutOrderEvent;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
class CheckoutOrderListener
{
    public function __construct()
    {
        //
    }

    public function handle(CheckoutOrderEvent $event)
    {
        $event->data->notify(New AppNotificationsCheckoutOrder());
    }
}

I had add use IlluminateNotificationsNotifiable; in model order

But, when executed, there exist error : Call to undefined method IlluminateDatabaseQueryBuilder::notify()

Is there anyone can help me?

Update

The full error is like this :

BadMethodCallException in Builder.php line 2440: Call to undefined method IlluminateDatabaseQueryBuilder::notify()

    in Builder.php line 2440
    at Builder->__call('notify', array(object(CheckoutOrder)))
    at call_user_func_array(array(object(Builder), 'notify'), array(object(CheckoutOrder))) in Builder.php line 1438
    at Builder->__call('notify', array(object(CheckoutOrder)))
    at call_user_func_array(array(object(Builder), 'notify'), array(object(CheckoutOrder))) in compiled.php line 11602
    at Model->__call('notify', array(object(CheckoutOrder))) in CheckoutOrderListener.php line 33
    at CheckoutOrderListener->handle(object(CheckoutOrderEvent))
    at call_user_func_array(array(object(CheckoutOrderListener), 'handle'), array(object(CheckoutOrderEvent))) in compiled.php line 10127
    at Dispatcher->IlluminateEvents{closure}(object(CheckoutOrderEvent))
    at call_user_func_array(object(Closure), array(object(CheckoutOrderEvent))) in compiled.php line 10067
    at Dispatcher->fire('AppEventsCheckoutOrderEvent') in compiled.php line 6290
    at Facade::__callStatic('fire', array(object(CheckoutOrderEvent))) in OrderController.php line 68
    at OrderController->create()
    at call_user_func_array(array(object(OrderController), 'create'), array()) in compiled.php line 9385
    at Controller->callAction('create', array()) in compiled.php line 9412
    at ControllerDispatcher->dispatch(object(Route), object(OrderController), 'create') in compiled.php line 8470
    at Route->runController() in compiled.php line 8451
    at Route->run(object(Request)) in compiled.php line 8147
    at Router->IlluminateRouting{closure}(object(Request)) in Pipeline.php line 53
    at Pipeline->IlluminateRouting{closure}(object(Request)) in compiled.php line 9978
    at SubstituteBindings->handle(object(Request), object(Closure)) in compiled.php line 9870
    at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 33
    at Pipeline->IlluminateRouting{closure}(object(Request)) in compiled.php line 3151
    at VerifyCsrfToken->handle(object(Request), object(Closure)) in compiled.php line 9870
    at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 33
    at Pipeline->IlluminateRouting{closure}(object(Request)) in compiled.php line 13467
    at ShareErrorsFromSession->handle(object(Request), object(Closure)) in compiled.php line 9870
    at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 33
    at Pipeline->IlluminateRouting{closure}(object(Request)) in compiled.php line 11907
    at StartSession->handle(object(Request), object(Closure)) in compiled.php line 9870
    at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 33
    at Pipeline->IlluminateRouting{closure}(object(Request)) in compiled.php line 13213
    at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in compiled.php line 9870
    at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 33
    at Pipeline->IlluminateRouting{closure}(object(Request)) in compiled.php line 13150
    at EncryptCookies->handle(object(Request), object(Closure)) in compiled.php line 9870
    at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 33
    at Pipeline->IlluminateRouting{closure}(object(Request)) in compiled.php line 9855
    at Pipeline->then(object(Closure)) in compiled.php line 8148
    at Router->runRouteWithinStack(object(Route), object(Request)) in compiled.php line 8139
    at Router->dispatchToRoute(object(Request)) in compiled.php line 8130
    at Router->dispatch(object(Request)) in compiled.php line 2472
    at Kernel->IlluminateFoundationHttp{closure}(object(Request)) in Pipeline.php line 53
    at Pipeline->IlluminateRouting{closure}(object(Request)) in compiled.php line 3213
    at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in compiled.php line 9870
    at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 33
    at Pipeline->IlluminateRouting{closure}(object(Request)) in compiled.php line 9855
    at Pipeline->then(object(Closure)) in compiled.php line 2416
    at Kernel->sendRequestThroughRouter(object(Request)) in compiled.php line 2400
    at Kernel->handle(object(Request)) in index.php line 53

My CheckoutOrderEvent is like this :

<?php
namespace AppEvents;
use IlluminateBroadcastingChannel;
use IlluminateQueueSerializesModels;
use IlluminateBroadcastingPrivateChannel;
use IlluminateBroadcastingPresenceChannel;
use IlluminateBroadcastingInteractsWithSockets;
use IlluminateContractsBroadcastingShouldBroadcast;
class CheckoutOrderEvent
{
    use InteractsWithSockets, SerializesModels;
    public $data;

    public function __construct($data)
    {
        $this->data = $data;
    }
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}

My CheckoutOrder is like this :

<?php
namespace AppNotifications;
use IlluminateBusQueueable;
use IlluminateNotificationsNotification;
use IlluminateContractsQueueShouldQueue;
use IlluminateNotificationsMessagesMailMessage;
class CheckoutOrder extends Notification
{
    use Queueable;
    public function __construct()
    {
        //
    }
    public function via($notifiable)
    {
        return ['mail'];
    }
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', 'https://laravel.com')
                    ->line('Thank you for using our application!');
    }
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

My model order is like this :

use Eloquent as Model;
use IlluminateDatabaseEloquentSoftDeletes;
use IlluminateNotificationsNotifiable;

class Order extends Model
{
    use SoftDeletes;
..........
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As from the discussion I found out that you did not include the Notifiable trait in your Order model. Do this instead:

use Eloquent as Model;
use IlluminateDatabaseEloquentSoftDeletes;
use IlluminateNotificationsNotifiable;

class Order extends Model
{
    use SoftDeletes, Notifiable;
    ..........

By including it, your model should now have the notify method as it should, and so can be used in the Notification system.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...