通过使用通知

时间:2019-05-29 16:47:40

标签: php json laravel

/ *我在向成员提供会议后试图检索通知,这种检索方式enter code here带有未定义的索引ID

我尝试使用json_encode,然后在检索过程中使用json_decode,但没有任何效果(我在使用json方面没有太多技巧)* /

/ *控制器      * /

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;
use App\Meeting;
use App\Meeting_agenda;
use App\Meeting_minute;
use App\Notifications\MeetingNotify;
use App\User;

class MeetingsController extends Controller
{
    public function store(Request $request, Meeting $meet)
    {
        $meeting=new Meeting();
        $meeting->meeting_title=$request->meeting_title;
        $meeting->meeting_venue=$request->meeting_venue;
        $meeting->meeting_date=$request->meeting_date;
        $meeting->meeting_type_id=$request->meeting_type_id;
        $meeting->save();

        foreach($request->input('name') as $agenda) {
            $agendas= new Meeting_agenda();
            $agendas->meeting_id=$meeting->id;
            $agendas->name=$agenda;
            $agendas->save();

        }
        $meet=Meeting::where('id',$meeting->id)->get();

                 $users=User::all();
                json_encode(Notification::send($users, new 
        MeetingNotify($meet))) ;

        return redirect('/meetings');
    }
}

/ *我的通知类别* /

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class MeetingNotify extends Notification
{
    use Queueable;
    protected $meet;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($meet)
    {
        $this->meet=$meet;

    }

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



    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */

     public function toDatabase($notifiable)
    {
        return [
            'meet'=>$this->meet,
            // 'user'=>auth()->user()
        ];
    }


     public function toArray($notifiable)
    {
        return [
        // 'meet'=>$this->meet,
        ];
    }

}

/ *我的后视刀片* /

<a href="#">
    @if((count(auth()->user()->unreadNotifications)>0))
      @foreach(auth()->user()
         ->unreadNotifications as $notification)
             <div class="dropdown-item"
               onclick="markAsReadNotification()">
                 @include('includes.notifications.'.snake_case(class_basename($notification->type)))

    </div>
     @endforeach
       @else
        <p>  No notifications!</p>
          @endif
            </a>

/ * includes.notifications.meeting_notify.blade.php * /

<a href="/Posts/{{$notification->data['meet']['id']}}">
    {{json_decode($notification->data['meet']['id'])}}
    </a>

/ *进入数据库的数据* /

{"meet":[{"id":43,"meeting_title":"ordinary","meeting_type_id":2,"meeting_venue":"sdsd","meeting_date":"2019-5-30","minute":null,"created_at":"2019-05-29 15:53:58","updated_at":"2019-05-29 15:53:58"}]}

/ *我希望将会议显示在我的包含刀片中,以便在那里重定向每个用户

下面是错误* /

    ErrorException (E_ERROR)
    Undefined index: id (View: C

:\xampp\htdocs\BoardMeetingSytem\resources\views\includes\notifications\meeting_notify.blade.php) (View: C:\xampp\htdocs\BoardMeetingSytem\resources\views\includes\notifications\meeting_notify.blade.php) (View: C:\xampp\htdocs\BoardMeetingSytem\resources\views\includes\notifications\meeting_notify.blade.php)
    Previous exceptions
    Undefined index: id (View: C:\xampp\htdocs\BoardMeetingSytem\resources\views\includes\notifications\meeting_notify.blade.php) (View: C:\xampp\htdocs\BoardMeetingSytem\resources\views\includes\notifications\meeting_notify.blade.php) (0)
    Undefined index: id (View: C:\xampp\htdocs\BoardMeetingSytem\resources\views\includes\notifications\meeting_notify.blade.php) (0)
    Undefined index: id (0)

1 个答案:

答案 0 :(得分:0)

您的问题是,当您应该使用get()时,您正在使用first()get()返回项目的集合,而first()将返回单个资源。

您的代码应如下所示:

$meet=Meeting::where('id',$meeting->id)->first();

更改后,一切都会正常运行