当前位置: 首页 > 知识库问答 >
问题:

不适用于laravel的注释通知

栾瑞
2023-03-14

错误在此输入图像描述

我试图发送事件的通知,当一些喜欢和评论他的帖子,通知评论和喜欢在这里工作是我的通知类。

class NewCommentEvent extends Notification
{

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

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

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toDatabase($notifiable)
    {
        return [
            'comment' => $this->comment,
            'event' => Event::find($this->comment->event_id),
            'user' => User::find($this->comment->user_id)
        ];
    }

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

我的控制器功能代码,用于通知评论

 public function store(CommentRequest $request)
    {


        $event = Event::findOrFail($request->event_id);


        Comment::create([
            'comment' => $request->comment,
            'user_id' => Auth::id(),
            'event_id' => $event->id
        ]);

        if ($event->user_id != $comment->user_id) {
            $user = User::find($event->user_id);
            $user->notify(new NewCommentEvent($comment));
        }



        Toastr::success('Comment post with success','', ["positionClass" => "toast-top-center"]);
        return redirect()->back();
    }

我的请求

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;

class CommentRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'comment' => 'required|max:2000',
        ];
    }
}

共有3个答案

王飞英
2023-03-14

错误信息很清楚<代码>$comment未定义。将控制器代码替换为以下代码:

public function store(CommentRequest $request)
    {


    $event = Event::findOrFail($request->event_id);

   // defined comment here
    $comment = Comment::create([
        'comment' => $request->comment,
        'user_id' => Auth::id(),
        'event_id' => $event->id
    ]);

    if ($event->user_id != $comment->user_id) {
        $user = User::find($event->user_id);
        $user->notify(new NewCommentEvent($comment));
    }



    Toastr::success('Comment post with success','', ["positionClass" => "toast-top-center"]);
    return redirect()->back();
}
秦鹏飞
2023-03-14

您尚未定义$comment,只是创建了一条注释。这是抛出错误

$comment = Comment::create([
     .
     .
]);

这会解决你的问题

禄仲渊
2023-03-14

在控制器中:未定义变量$comment

来自Laravel文档:

create方法返回保存的模型实例。

因此,解决方案是:

$comment = Comment::create([
            'comment' => $request->comment,
            'user_id' => Auth::id(),
            'event_id' => $event->id
        ]);
 类似资料:
  • 我是Spring的新手,我很困惑@CreatedDate注释在实体中是如何工作的。 我做了一次谷歌搜索,有很多解决方案,但除了一个,没有一个适合我。我很困惑为什么? 这是我先试的 它不起作用。我为列中的值获取了NULL。 然后我做了这个。 这实际上将时间戳存储在db中。我的问题是,我遵循的大多数教程都建议我不需要来获取当前时间戳。看起来我确实需要它。我缺少什么吗?

  • 我试图让RunWith(PowerMockRunner.class)处理我现有的包注释。 版本: Powermock 1.4.12 mockito 1.9.0 jUnit 4.8.2 package-info.java//这是包注释 测试说明。java//这是包“com.smin.dummy”的元数据注释类 A、 爪哇 莫卡。Java语言 在unitest MockA中。如果我不使用RunWith

  • 环境:科特林 1.5.30, 春靴 2.5.4(Spring 5.3.9) 我试图创建一个组合注释来简化类似的模板注释代码。注释类如下: 预期用法: application.yaml: 但在应用程序启动后,TheBean 未按预期注册。 > 首先,我在github spring存储库中搜索,发现了这个:回归:自定义组合@Profile注释,组件扫描不再支持没有运行时保留。所以我试图但没有效果。 尝

  • 我已经编写了整型适配器,并用作注释来实现以下输出,但在gson库中编写的代码完全不允许使用注释配置进行空处理。 如果我取消注释1,2,4行和注释3行,那么我仍然没有实现我的愿望输出。 当我调试第189行的库代码(给定的屏幕截图)时,没有到达我的自定义适配器代码,因此空处理无法使用注释。有没有其他方法可以根据注释配置实现所需的输出? 期望输出: 猫科动物 - "" (用作注释并具有空值) phone

  • Spring参考文档说明如下: ComponentScanPackageMarker类故意省略了@Configuration注释。我已经测试了组件扫描和自动连接功能。令我吃惊的是,一切都很顺利: 组件扫描的这种行为是故意的吗?为什么即使没有@Configuration注释也能工作?

  • 我正在开发一个示例SpringBoot应用程序。我有两个包裹。通用域名格式。A(其中有@springbootApplication注释的主类)2。通用域名格式。B(其他春豆)。 现在我的问题是:由于应用程序失败,包B中的Spring bean没有被扫描。我尝试使用a。@springbootApplication atio(scanBasePackages="com. B")b。也@组件扫描(...