【问题标题】:Notification Using Task Scheduling Laravel使用 Laravel 任务调度的通知
【发布时间】:2020-06-10 09:21:36
【问题描述】:

这是我的 DeviceNotification 命令

<?php

namespace App\Console\Commands;

use App\Notifications\WeatherNotification;
use Illuminate\Console\Command;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Carbon;
use Zttp\Zttp;
use Notification;




class DeviceNotification extends Command
{


protected $signature = 'Device:Notification';


protected $description = 'Making notification everyday with per device(User) with weather APi';

public function __construct()
{
    parent::__construct();
}


public function handle()
{

    $allusers = DB::table('users')->where('account_type', 'farm_manager')->get();

    $apiKey = 'cde1hjdsgfhbssb051njidsn65c90';

    foreach ($allusers as $userone) {

        $Lat = DB::table('users')->where('user_id', $userone->user_id)->value('x_loc');
        $Lat = number_format($Lat, 4, '.', ',');

        $Long = DB::table('users')->where('user_id', $userone->user_id)->value('y_loc');
        $Long = number_format($Long, 4, '.', ',');

        $response = Zttp::get("https://api.darksky.net/forecast/$apiKey/$Lat,$Long?units=ca");
        $data = $response->json();

        $rainprediction =  100 * $data['daily']['data']['0']['precipProbability'];

        $tomoprediction =  100 * $data['daily']['data']['1']['precipProbability'];

        $dayafteroprediction =  100 * $data['daily']['data']['2']['precipProbability'];


        if ($rainprediction > 70.0) {
            $msg = "There is " . $rainprediction . "% probability of rainfall today.";
        } elseif ($rainprediction > 50.0 && $rainprediction < 70.0) {
            $msg = "There is " . $rainprediction . "% probability of rainfall today.";
        } else {
            $msg = "There is " . $rainprediction . "% probability of rainfall today.";
        }
        if ($tomoprediction > 70.0) {
           $msgtomoprediction = "There is " . $tomoprediction . "% probability of  Tmorrow.";
        } elseif ($tomoprediction > 50.0 && $tomoprediction < 70.0) {
          $msgtomoprediction = "There is " . $tomoprediction . "% probability of Tomorrow.";
        } else {
          $msgtomoprediction = "There is " . $tomoprediction . "% probability of Tomorrow.";
        }
        if ($dayafteroprediction > 70.0)
         {
            $msgdayafteroprediction = "There is " . $dayafteroprediction . "%  After Tomorrow.";
          } elseif ($dayafteroprediction > 50.0 && $dayafteroprediction < 70.0) { 
           $msgdayafteroprediction = "There is " . $dayafteroprediction . "% probability Tomorrow.";
        } else {
         $msgdayafteroprediction = "There is " . $dayafteroprediction . "%  Tomorrow.";
        }

        $user_id = $userone->farm_id;
        $message_notification = array(
            'today' => $msg,
            'Tomorrow' => $msgtomoprediction,
            'Day-After' => $msgdayafteroprediction,
        );



        //$userone->notify(new WeatherNotification($user_id, $message_notification));
        Notification::send($userone, new WeatherNotification($user_id, $message_notification));
    }
}
}

这是我的通知

 <?php

 namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use  App\User;

 class WeatherNotification extends Notification
{
use Queueable;

/**
 * Create a new notification instance.
 *
 * @return void
 */
protected $user_id;
protected $message_notification;

public function __construct($user_id, $message_notification)
{
    //
    $this->user_id = $user_id;
    $this->message_notification = $message_notification;
}

/**
 * Get the notification's delivery channels.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return ['database'];
}


public function toArray($notifiable)
{

    return [
        //

        'farm_id' =>  $this->user_id,
        'weather_notification' =>  $this->message_notification,
    ];
}
}

我得到 Symfony\Component\Debug\Exception\FatalThrowableError : 调用未定义的方法 stdClass::notify()
当我使用 $userone->notify(new WeatherNotification($user_id, $message_notification));

运行代码时出现此错误

这个: Symfony\Component\Debug\Exception\FatalThrowableError : 调用未定义的方法 stdClass::routeNotificationFor()

当我使用此 Notification::send($userone, new WeatherNotification($user_id, $message_notification)); 运行它时;

【问题讨论】:

    标签: php laravel notifications task


    【解决方案1】:

    在您的用户模型上使用可通知特征。

    use Illuminate\Notifications\Notifiable;
    

    这是它在文档中的描述。 using-the-notifiable-trait

    也使用用户模型而不是查询生成器来检索用户

    User::where('account_type', 'farm_manager')->get();
    

    【讨论】:

    • 试过了!同样的错误
    • 您实际上是在使用查询生成器来检索用户,这就是未定义方法 notify 的原因。所以使用 User:: 而不是 DB::table('users')
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 2021-10-22
    • 1970-01-01
    • 2021-10-09
    • 2020-02-25
    • 2017-10-11
    • 2012-10-06
    相关资源
    最近更新 更多