【问题标题】:How to retrieve the data sent to database after sent by using notification使用通知发送后如何检索发送到数据库的数据
【发布时间】:2019-10-15 08:10:20
【问题描述】:

/* 我在向成员提供会议后尝试检索通知,检索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>

/* 包括.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)

【问题讨论】:

    标签: php json laravel


    【解决方案1】:

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

    您的代码应如下所示:

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

    更改后一切正常

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-23
      • 2016-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多