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

Ryan Sacks :

I'm building a Job Search type of application. 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. 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." with the 2 date and time options. The Job Seeker selects date and time option 1, clicks send and then the employer receives the notification from the job seeker. 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. 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. 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:

{!! 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:

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?

miken32 :

Your error is because you're pulling every single JobSeekerProfile into a collection and then trying to call notify() on the collection. Since you're submitting the ID, just use that to build an instance and notify it.

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');

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=216973&siteId=1