简体   繁体   English

如何在 Laravel 中向当前未登录的用户发送通知?

[英]How can I send a notification in Laravel to a user that is not currently logged in?

I'm building a Job Search type of application.我正在构建一个 Job Search 类型的应用程序。 There are two types of users, job seekers and employers where each has their own Role.有两种类型的用户,求职者和雇主,每个人都有自己的角色。 When an Employer is logged in he can browse through the profiles of candidates and request an interview.当雇主登录后,他可以浏览候选人的个人资料并请求面试。 He can choose 2 options for time and date and click send.他可以为时间和日期选择 2 个选项,然后单击发送。 When that specific User signs in, at the top of the page I want to display the notifications with a bell icon and the notification should say "You've been sent an Interview request!".当该特定用户登录时,我想在页面顶部显示带有铃铛图标的通知,并且通知应显示“您已收到面试请求!”。 Then when they click it or go to the Notification section on the admin panel, it will display the Employers information "Hi username, Company ABC is interested in your profile and would like to schedule you for an interview."然后,当他们单击它或转到管理面板上的“通知”部分时,它将显示雇主信息“您好,用户名,ABC 公司对您的个人资料感兴趣,并希望为您安排面试。” with the 2 date and time options.带有 2 个日期和时间选项。 The Job Seeker selects date and time option 1, clicks send and then the employer receives the notification from the job seeker.求职者选择日期和时间选项 1,点击发送,然后雇主会收到求职者的通知。 They will communicate this way until they both agree on a date and time to setup an interview.他们将通过这种方式进行沟通,直到双方就安排面试的日期和时间达成一致。 So I would like 2 notifications for this.所以我想要 2 个通知。 1 when the interview request is sent to a candidate and a second for when a candidate select a date and time that is good for him and sends the request back to the employer. 1 当面试请求被发送给候选人时,第二次是当候选人选择一个对他有利的日期和时间并将请求发送回雇主时。 I understand how to send a notification when a User is logged in, but I'm missing something for how to send a notification to a user that is not logged.我了解如何在用户登录时发送通知,但我缺少有关如何向未登录用户发送通知的内容。 Both of these types of users are stored in my Users Table, just with different roles.这两种类型的用户都存储在我的用户表中,只是角色不同。

job_seeker_profile.blade.php file: job_seeker_profile.blade.php 文件:

{!! Form::open(['method'=>'POST', 'action'=>'AdminEmployerInterviewRequestsController@store', 'files'=>true, 'style'=>'width: 100%;']) !!}

<div class="form-group">
    <div class="input-group date" id="datetimepicker1" data-target-input="nearest">
        {!! Form::text('date_time1', null, ['class'=> $errors->first('date_time1') ? 'border-danger form-control datetimepicker-input' : 'form-control datetimepicker-input', 'data-target'=>'#datetimepicker1']) !!}
        <div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker">
            <div class="input-group-text"><i class="fa fa-calendar"></i></div>
        </div><br>
    </div>
    <small class="text-danger">{{ $errors->first('date_time1') }}</small>
</div>
<div class="col">

</div>

<div class="form-group">
    <div class="input-group date" id="datetimepicker2" data-target-input="nearest">
        {!! Form::text('date_time2', null, ['class'=> $errors->first('date_time2') ? 'border-danger form-control datetimepicker-input' : 'form-control datetimepicker-input', 'data-target'=>'#datetimepicker2']) !!}
        <div class="input-group-append" data-target="#datetimepicker2" data-toggle="datetimepicker">
            <div class="input-group-text"><i class="fa fa-calendar"></i></div>
        </div><br>
    </div>
    <small class="text-danger">{{ $errors->first('date_time2') }}</small>
</div>

<div class="form-group">
    {!! Form::hidden('user_id', Auth::user()->id, ['class'=>'form-control']) !!}
</div>

<div class="form-group">
    {!! Form::hidden('job_seeker_profile_user_id', $jobSeekerProfile->id, ['class'=>'form-control']) !!}
</div>

<div class="form-group">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
    {!! Form::submit('Send Interview Request', ['class'=>'btn btn-primary float-right']) !!}
</div>
<br><br><br><br>

{!! Form::close() !!}

AdminEmployerInterviewRequestsController.php file: AdminEmployerInterviewRequestsController.php 文件:

public function store(EmployerInterviewCreateRequest $request)
{
    $input = $request->all();

    $user = Auth::user();

    $JobSeekerProfile = JobSeekerProfile::all();

    $user->interviewRequestsSent()->create($input);
    $user->interviewRequestsReceived()->create($input);

    $user->notify(new InterviewRequestSent());

    $JobSeekerProfile->notify(new InterviewRequestReceived());

    return redirect('/admin/employer/interviews');

}

but when I called the但是当我打电话给

$JobSeekerProfile->notify(new InterviewRequestReceived());

It gave me this error:它给了我这个错误:

Method Illuminate\Database\Eloquent\Collection::notify does not exist.

Is it possible to send another user a notification after an action is taken?是否可以在采取行动后向其他用户发送通知?

It's not possible right now.现在不可能。 Only users who are logged in can receive a notification.只有登录的用户才能收到通知。 I've sent several tickets regarding this我已经发送了几张关于这个的票

Your error is because you're pulling every single JobSeekerProfile into a collection and then trying to call notify() on the collection.您的错误是因为您将每个JobSeekerProfile拉入一个集合,然后尝试在该集合上调用notify() Since you're submitting the ID, just use that to build an instance and notify it.由于您正在提交 ID,只需使用它来构建一个实例并通知它。

public function store(EmployerInterviewCreateRequest $request)
{
    $input = $request->all();

    $user = Auth::user();

    $JobSeekerProfile = JobSeekerProfile::find($request->job_seeker_profile_user_id);

    $user->interviewRequestsSent()->create($input);
    $user->interviewRequestsReceived()->create($input);

    $user->notify(new InterviewRequestSent());

    $JobSeekerProfile->notify(new InterviewRequestReceived());

    return redirect('/admin/employer/interviews');

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想在发票状态更新/更改时向当前登录的用户发送邮件我该怎么做? - i want to send a mail to the currently logged in user when the invoice status is updated/changed how can i do it? 如何显示当前登录 CodeIgniter 的数据用户 - How can I show data user currently logged in in CodeIgniter Laravel 如何根据当前登录用户自定义用户配置文件 url - Laravel how to customize user profile url based on currently logged in user 如何只显示当前登录用户的清单-Laravel - How can you only show the checklists from the currently logged in user - Laravel 如何在 Laravel 中显示属于已登录用户的项目? - How can I show items belonging to a logged in user in Laravel? 如何在cakePHP中的函数beforeDelete()中获取当前登录用户的id? - How can I get the id of currently logged-in user in function beforeDelete() in cakePHP? 如何检查用户当前是否正在登录并正在玩Facebook应用程序游戏 - How can I check if a user is currently logged in and playing a facebook app game 如何从单独的PHP文件访问当前登录的Wordpress用户? - How can I access the currently logged in Wordpress user from a separate PHP file? Laravel:检查当前是否有其他用户登录 - Laravel : Check if another user is currently logged in Laravel更新当前已登录用户的模型 - Laravel update currently logged in user's model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM