本文介绍了如何解决对未定义方法Illuminate \ Database \ Query \ Builder :: 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));
}

我的CheckoutOrderListener是这样的:

My CheckoutOrderListener is like this :

<?php
namespace App\Listeners;
use App\Events\CheckoutOrderEvent;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class CheckoutOrderListener
{
    public function __construct()
    {
        //
    }

    public function handle(CheckoutOrderEvent $event)
    {
        $event->data->notify(New \App\Notifications\CheckoutOrder());
    }
}

我已经按模型顺序添加了Illuminate\Notifications\Notifiable;用途

I had add use Illuminate\Notifications\Notifiable; in model order

但是,执行时会出现错误:Call to undefined method Illuminate\Database\Query\Builder::notify()

But, when executed, there exist error : Call to undefined method Illuminate\Database\Query\Builder::notify()

有没有人可以帮助我?

更新

完整错误是这样的:

BadMethodCallException in Builder.php line 2440: Call to undefined method Illuminate\Database\Query\Builder::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->Illuminate\Events\{closure}(object(CheckoutOrderEvent))
    at call_user_func_array(object(Closure), array(object(CheckoutOrderEvent))) in compiled.php line 10067
    at Dispatcher->fire('App\Events\CheckoutOrderEvent') 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->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 53
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in compiled.php line 9978
    at SubstituteBindings->handle(object(Request), object(Closure)) in compiled.php line 9870
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in compiled.php line 3151
    at VerifyCsrfToken->handle(object(Request), object(Closure)) in compiled.php line 9870
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in compiled.php line 13467
    at ShareErrorsFromSession->handle(object(Request), object(Closure)) in compiled.php line 9870
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in compiled.php line 11907
    at StartSession->handle(object(Request), object(Closure)) in compiled.php line 9870
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in compiled.php line 13213
    at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in compiled.php line 9870
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in compiled.php line 13150
    at EncryptCookies->handle(object(Request), object(Closure)) in compiled.php line 9870
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
    at Pipeline->Illuminate\Routing\{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->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
    at Pipeline->Illuminate\Routing\{closure}(object(Request)) in compiled.php line 3213
    at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in compiled.php line 9870
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
    at Pipeline->Illuminate\Routing\{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

我的CheckoutOrderEvent是这样的:

My CheckoutOrderEvent is like this :

<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class CheckoutOrderEvent
{
    use InteractsWithSockets, SerializesModels;
    public $data;

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

我的CheckoutOrder是这样的:

My CheckoutOrder is like this :

<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
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 [
            //
        ];
    }
}

我的模型订单如下:

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;

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

推荐答案

从讨论中我发现,您没有在Order模型中包含Notifiable特性.改为执行此操作:

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 Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;

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

通过包含它,您的模型现在应该具有应有的notify方法,因此可以在通知系统中使用.

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

这篇关于如何解决对未定义方法Illuminate \ Database \ Query \ Builder :: notify()的调用? (laravel 5.3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 18:07