问题
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' => 'chelsea@gmail.com'
);
$data = Order::where('number', $data['number'])->first();
\Event::fire(new CheckoutOrderEvent($data));
}
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());
}
}
I had add use Illuminate\Notifications\Notifiable;
in model order
But, when executed, there exist error : Call to undefined method Illuminate\Database\Query\Builder::notify()
Is there anyone can help me?
Update
The full error is like this :
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
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');
}
}
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 [
//
];
}
}
My model order is like this :
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
class Order extends Model
{
use SoftDeletes;
..........
回答1:
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;
..........
By including it, your model should now have the notify
method as it should, and so can be used in the Notification system.
回答2:
I also got this error while put a query like
$data = DB::abcd("Select * from xyz"); ,(abcd->filename)
but we need to put
$data = DB::select("Select * from xyz");
来源:https://stackoverflow.com/questions/41627259/how-to-solve-call-to-undefined-method-illuminate-database-query-buildernotify