Laravel 8:在字符串上调用成员函数notify() [英] Laravel 8: Call to a member function notify() on string

查看:58
本文介绍了Laravel 8:在字符串上调用成员函数notify()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与Laravel 8合作创建一个论坛,现在我想向有人问了问题的用户发送通知.

因此,为了做到这一点,我将其添加到了控制器中:

 公共函数postAnswer(请求$ request,$ question,$ asker){$ validate_data = $ request-> validate(['answer'=>'必需的',]);$ answer =答案::创建(['answer'=>$ request-> answer,'user_id'=>auth()-> user()-> id,'question_id'=>$ question,]);$ asker-> notify(new RepliedToThread($ question));返回back();} 

因此, $ question 问题ID ,而 $ asker 是该用户的用户ID 问了这个问题.

但是现在我得到了这个错误:

在字符串上调用成员函数notify()

现在有趣的部分是,当我使用 auth()-> user()-> notify(new RepliedToThread($ question)); 时,它工作正常!

但是我不想通知任何经过身份验证的用户,我需要通知询问问题的人,这就是为什么我尝试传递 $ asker ...

所以,如果您知道如何解决此问题,请告诉我...

我真的很感谢你们的任何想法或建议.

谢谢.

刀片:

 < form action =" {{route('questions.answers',['question'=> $ show-> id,'asker'==> $ show->用户-> id])}}"方法=POST"enctype ="multipart/form-data".@csrf< div class ="mb-4">< textarea name =" answer"id ="answer"class ="form-control BSinaBold"行="7"占位符=写下答案"</textarea>@error('answer')< div class ="text-red-500 mt-2 text-sm">{{$ message}}</div>@enderror</div></br>< button type =提交";class ="btn btn-primary">发送"/按钮";</form> 

解决方案

问题是 asker 只是一个ID,您应该获取 asker 的其他信息从用户表中,然后将您的通知传递给 asker .

  $ asker =用户:: findOrFail($ asker);$ asker-> notify(new RepliedToThread($ question)); 

I'm working with Laravel 8 to make a forum and now I wanted to send a notification to a user who has asked a question whenever someone answered that question.

So in order to do that, I added this to the Controller:

public function postAnswer(Request $request, $question, $asker) {
        $validate_data = $request->validate([
            'answer' => 'required',
        ]);

        $answer = Answer::create([
            'answer' => $request->answer,
            'user_id' => auth()->user()->id,
            'question_id' => $question,
        ]);

        $asker->notify(new RepliedToThread($question));

        return back();
    }

So the $question is the question id and $asker is the user id of the person who has asked the question.

But now I get this error:

Call to a member function notify() on string

Now the interesting part is that, when I use auth()->user()->notify(new RepliedToThread($question));, it is working fine!

But I don't want to notify any authenticated user, I need to notify the person who has asked question, that's why I tried passding $asker...

So if you know how to solve this question, please let me know...

I would really appreciate any idea or suggestion from you guys.

Thanks in advance.

Blade:

<form action="{{ route('questions.answers', ['question' => $show->id, 'asker' => $show->user->id]) }}" method="POST" enctype="multipart/form-data">
    @csrf
    <div class="mb-4">
        <textarea name="answer" id="answer" class="form-control BSinaBold" rows="7" placeholder="Write down answer"></textarea>
        @error('answer')
            <div class="text-red-500 mt-2 text-sm">
                {{ $message }}
            </div>
        @enderror
    </div>
    </br>
    <button type="submit" class="btn btn-primary">Send</button>
</form>

解决方案

The problem is that asker is just an id, you should take the other information of the asker from the user table and after that pass your notification to the asker.

$asker = User::findOrFail($asker);
$asker->notify(new RepliedToThread($question));

这篇关于Laravel 8:在字符串上调用成员函数notify()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆