ErrorException 未定义变量 $employee

时间:2021-02-12 17:57:48

标签: laravel

使用数据库通知我已将通知存储在通知表中。

但现在我想在刀片模板中显示通知。

我遇到了一个未定义的错误。

控制器

$data=Employee::create([
    'first_name'=>$request->input('first_name'),
    'last_name'=>$request->input('last_name'),
    'username'=>$request->input('username'),
    'email'=>$request->input('email'),
    'password'=>$request->input('password'),
    'confirm_password'=>$request->input('confirm_password'),
]);


$admin=Employee::find(1);

$admin->notify(new NotifyAdmin($data));

NotifyAdmin 类

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Notifiable;
use App\Models\Employee; 


class NotifyAdmin extends Notification implements ShouldQueue
{
    use Queueable, Notifiable;
    private $val;

   
    public function __construct(Employee $employee)
    {
       
        $this->val=$employee;
    }

    public function via($notifiable)
    {
        return ['database'];
    }

   
    public function toArray($notifiable)
    {
        return [
            'username'=> $this->val->username
        ];
    }
}

执行此操作后,通知将存储在通知表中。 现在我想显示此通知

<div class="media-body">
    @foreach($employee->notifications as $row)
        <p class="noti-details"><span class="noti-title">Admin</span> added new doctor <span class="noti-title">{{$row->data['username']}}</span></p>
        <p class="noti-time"><span class="notification-time">4 mins ago</span></p>
    @endforeach
</div>

1 个答案:

答案 0 :(得分:0)

这是我的控制器

    <?php

namespace App\Http\Controllers;

use App\Models\Employee;
use Illuminate\Http\Request;
use App\helpers\helper;
use App\Notifications\NotifyAdmin;


class EmployeeController extends Controller
{
    
   
    

   

     public function InsertEmployees(Request $request)
        {
          
            $data=Employee::create([
                'first_name'=>$request->input('first_name'),
                'last_name'=>$request->input('last_name'),
                'username'=>$request->input('username'),
                'email'=>$request->input('email'),
                'password'=>$request->input('password'),
                'confirm_password'=>$request->input('confirm_password'),
                'phone'=>$request->input('phone'),
                'nid_number'=>$request->input('nid_number'),
                'status'=>$request->input('status'),
                'roles'=>$request->input('roles'),
                'join_date'=>$request->input('join_date'),
    
            ]);
            
            $admin=Employee::find(1);
            
            $admin->notify(new NotifyAdmin($data));
相关问题