laravel:更新记录时发送带有(data)的通知

时间:2019-04-29 22:50:19

标签: laravel

问题:如何显示具有更新功能的数据?

已发送通知,但没有我在此函数中指定的数据:     公共函数toDatabase($ notifiable)     {         返回[             '数据'=> $ this-> booking-> num_ch            ];
    }

它可以与存储功能一起使用,但不能与更新功能一起使用

我的通知班:

<?php

namespace App\Notifications;


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

class NewMessage extends Notification
{
    use Queueable;
    public $booking;

    public function __construct(Booking $booking)
    {
        //
        $this->booking = $booking;

    }


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


    public function toDatabase($notifiable)
    {
        return [
            'data'=>$this->booking->num_ch
           ];                  
    }



我的更新功能:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Booking;
use App\User;
use Notification;
use App\Notifications\NewMessage;

class DispoController extends Controller
{


    public function update(Request $request,$id,Booking $booking)
    {
        //  
         Booking::findOrFail($id)->update([
         'num_ch'=>$request->num_ch,
         'type'=>$request->type,
         'statut'=>$request->statut,
         'enfants'=>$request->enfants,
         'adultes'=>$request->adultes,


        ]);

        auth()->user()->notify(new NewMessage($booking)); // notification


      return redirect()->route('booking.index')->with(['success'=>'succés']);

    }


它仅显示“新预订”

 @foreach(Auth::user()->unreadNotifications as $not)
         <li>
          <a class="dropdown-item"  >new booking {{$not->data['data']}}</a>
         </li>
 @endforeach

2 个答案:

答案 0 :(得分:0)

问题是您没有捕获已保存的预订,应该是这样

public function update(Request $request,$id,Booking $booking)
{
    //  
     $booking = Booking::findOrFail($id)->update([
         'num_ch'=>$request->num_ch,
         'type'=>$request->type,
         'statut'=>$request->statut,
         'enfants'=>$request->enfants,
         'adultes'=>$request->adultes,
    ]);

    auth()->user()->notify(new NewMessage($booking)); // notification


  return redirect()->route('booking.index')->with(['success'=>'succés']);

}

因此,您从函数调用中传递了一个空的预订,并将其传递给了通知而不是更新的记录。

但是您可以通过这样做简化整个过程

public function update(Request $request, Booking $booking)
{
    //  
     $booking->update([
         'num_ch'=>$request->num_ch,
         'type'=>$request->type,
         'statut'=>$request->statut,
         'enfants'=>$request->enfants,
         'adultes'=>$request->adultes,
    ]);

    auth()->user()->notify(new NewMessage($booking)); // notification


    return redirect()->route('booking.index')->with(['success'=>'succés']);
}

因此,除了传递ID和预订信息外,只需传递预订信息即可,该信息应在路线和集装箱中自动找到。

答案 1 :(得分:0)

    public function update(Request $request,$id,Booking $booking)
    {
        //  
        Booking::findOrFail($id)->update([
            'num_ch'=>$request->num_ch,
            'type'=>$request->type,
            'statut'=>$request->statut,
            'enfants'=>$request->enfants,
            'adultes'=>$request->adultes,

        ]);
        auth()->user()->notify(new NewMessage(Booking::findOrFail($id)));

         // notification


        return redirect()->route('info_client.index')->with(['success'=>'succés']);
    }