【问题标题】:laravel Error "Call to undefined method stdClass::notify()" in laravel 8laravel 8 中的 laravel 错误“调用未定义的方法 stdClass::notify()”
【发布时间】:2021-09-13 12:52:03
【问题描述】:

一旦我验证了他们的帐户,我希望能够向用户发送电子邮件,但我一直有这个错误“调用未定义的方法 stdClass::notify()”,我不知道我错在哪里。请帮我看看我的代码并告诉我出了什么问题。 这是我的控制器.php

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Admin;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use RealRashid\SweetAlert\Facades\Alert;
use App\Notifications\AccountVerificationEmail;
use Illuminate\Support\Facades\Notification;
use Notifiable;

class UserController extends Controller
{   
 use \Illuminate\Notifications\Notifiable; 
 use \Illuminate\Notifications\RoutesNotifications;
 public function update_verify($id){
         $post = User::first();
        $post = DB::table('users')
                ->select('submitted')
                ->where('id', '=' ,$id )
                ->first();

                if ($post->submitted == 0) {
                     $submitted = 1;
                }
                else{
                     $submitted = 0;
                }
                $val = array('submitted' => $submitted); 
            DB::table('users')->where('id',$id)->update($val);

            $vermail =  DB::table('users')->where('id', '=', $id)->first();
            $vermail->notify(new AccountVerificationEmail());        

         return redirect('verify')->with('success','User has been verified!');
   }
}

我尝试使用这个“Notification::send($vermail, new AccountVerificationEmail());”代替“$vermail->notify(new AccountVerificationEmail());”但我仍然有错误

这是我的通知页面

<?php

namespace App\Notifications;

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

class AccountVerificationEmail extends Notification
{
    use Queueable;
    private $vermail;

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

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

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('Your account has been verified!')
                    ->action('Go to home page', url('/https://switfx.com/'))
                    ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

最后这是我的用户模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
 use \Illuminate\Notifications\Notifiable;
    use \Illuminate\Notifications\RoutesNotifications;
 

class User extends Model
{
     use \Illuminate\Notifications\Notifiable;
    use \Illuminate\Notifications\RoutesNotifications;
 
    use HasFactory, Notifiable;
}

【问题讨论】:

    标签: php laravel notifications laravel-8


    【解决方案1】:

    您使用的是返回标准对象的 DB::table,而不是添加所有额外功能的 Eloquent。改变这一行

    $vermail =  DB::table('users')->where('id', '=', $id)->first();
    

    $vermail = User::find($id);
    

    【讨论】:

      猜你喜欢
      • 2015-04-17
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 2020-06-02
      • 2014-11-07
      • 2012-04-02
      • 2017-12-12
      • 2022-11-03
      相关资源
      最近更新 更多