首页 > 解决方案 > Laravel:发送通知时不能序列化或反序列化 PDO 实例

问题描述

向通知添加队列时使用 Laravel 发送通知时出现问题。环顾了供应商类,但看不到这样做的原因我什至在任何地方都看不到对 PDO 的任何引用?如果有人可以帮助我们

堆栈跟踪:

(1/1) PDOException
You cannot serialize or unserialize PDO instances

in Queue.php line 139
at PDO->__sleep()
at serialize(object(SendQueuedNotifications))
in Queue.php line 139
at Queue->createObjectPayload(object(SendQueuedNotifications), 'queues:users')
in Queue.php line 110
at Queue->createPayloadArray(object(SendQueuedNotifications), 'queues:users', '')
in RedisQueue.php line 153
at RedisQueue->createPayloadArray(object(SendQueuedNotifications), 'queues:users', '')
in Queue.php line 88
at Queue->createPayload(object(SendQueuedNotifications), 'queues:users', '')
in RedisQueue.php line 91
at RedisQueue->push(object(SendQueuedNotifications))
in Dispatcher.php line 184
at Dispatcher->pushCommandToQueue(object(RedisQueue), object(SendQueuedNotifications))
in Dispatcher.php line 160
at Dispatcher->dispatchToQueue(object(SendQueuedNotifications))
in Dispatcher.php line 73
at Dispatcher->dispatch(object(SendQueuedNotifications))
in NotificationSender.php line 195
at NotificationSender->queueNotification(array(object(BelongsTo)), object(ThreadStartedWithProfileNotification))
in NotificationSender.php line 74
at NotificationSender->send(array(object(BelongsTo)), object(ThreadStartedWithProfileNotification))
in ChannelManager.php line 39
at ChannelManager->send(object(BelongsTo), object(ThreadStartedWithProfileNotification))
in Facade.php line 237
at Facade::__callStatic('send', array(object(BelongsTo), object(ThreadStartedWithProfileNotification)))
in UserNotification.php line 27
at UserNotification::send(object(BelongsTo), object(ThreadStartedWithProfileNotification))
in CreateController.php line 51
at CreateController->__invoke(object(Request), object(Profile))
at call_user_func_array(array(object(CreateController), '__invoke'), array(object(Request), object(Profile)))
in BoundMethod.php line 32
at BoundMethod::Illuminate\Container\{closure}()
in BoundMethod.php line 90
at BoundMethod::callBoundMethod(object(Application), array(object(CreateController), '__invoke'), object(Closure))
in BoundMethod.php line 34
at BoundMethod::call(object(Application), array(object(CreateController), '__invoke'), array('profile' => object(Profile)), null)
in Container.php line 576
at Container->call(array(object(CreateController), '__invoke'), array('profile' => object(Profile)))
in RoutesRequests.php line 376
at Application->callControllerCallable(array(object(CreateController), '__invoke'), array('profile' => object(Profile)))
in RoutesRequests.php line 342
at Application->callLumenController(object(CreateController), '__invoke', array(1, array('uses' => 'App\\Http\\Controllers\\Profile\\Messaging\\CreateController', 'middleware' => array('unsupported', 'unacceptable', 'auth:api')), array('profile' => object(Profile))))
in RoutesRequests.php line 316
at Application->callControllerAction(array(1, array('uses' => 'App\\Http\\Controllers\\Profile\\Messaging\\CreateController', 'middleware' => array('unsupported', 'unacceptable', 'auth:api')), array('profile' => object(Profile))))
in RoutesRequests.php line 278
at Application->callActionOnArrayBasedRoute(array(1, array('uses' => 'App\\Http\\Controllers\\Profile\\Messaging\\CreateController', 'middleware' => array('unsupported', 'unacceptable', 'auth:api')), array('profile' => object(Profile))))
in RoutesRequests.php line 258
at Application->Laravel\Lumen\Concerns\{closure}(object(Request))
at call_user_func(object(Closure), object(Request))
in Pipeline.php line 52
at Pipeline->Laravel\Lumen\Routing\{closure}(object(Request))
in Authenticate.php line 43
at Authenticate->handle(object(Request), object(Closure), 'api')
in Pipeline.php line 163
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request))
in Pipeline.php line 32


UserNotification::send($profile->user(), new ThreadStartedWithProfileNotification(
    $profile,
    $thread,
    $accountSending
));

用户通知:

<?php

declare(strict_types=1);

namespace Shared\Helpers;

use App\Models\User;
use RuntimeException;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Notification;
use Shared\Helpers\Contract\UserNotificationInterface;

/**
 * Class UserNotification
 * @package Shared\Helpers
 */
class UserNotification
{
    /**
     * @param $recipients
     * @param UserNotificationInterface $notification
     * @return void
     */
    public static function send($recipients, UserNotificationInterface $notification) : void
    {
        self::validate($recipients);
        Notification::send($recipients, $notification);
    }

    /**
     * @param $recipients
     * @param UserNotificationInterface $notification
     * @return void
     */
    public static function sendNow($recipients, UserNotificationInterface $notification) : void
    {
        self::validate($recipients);
        Notification::sendNow($recipients, $notification);
    }

    /**
     * @param Collection|User $recipients
     * @return void
     */
    private static function validate($recipients) : void
    {
        if ($recipients instanceof User) {
            return;
        }

        $recipients->each(function ($recipient) {
            if (! $recipient instanceof User) {
                throw new RuntimeException(sprintf(
                    'Recipient must be an instance of App\Models\User, %s given',
                    $recipient
                ));
            }
        });
    }
}

实际通知:

<?php

declare(strict_types = 1);

namespace App\Notifications\Individual;

use App\Models\Account;
use App\Models\Profile;
use App\Models\Thread;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Shared\Helpers\Contract\UserNotificationInterface;
use Shared\Helpers\Helper;

/**
 * Class ThreadStartedWithProfile  Notification
 * @package App\Notifications\Individual
 */
class ThreadStartedWithProfileNotification extends Notification implements ShouldQueue, UserNotificationInterface
{
    use Queueable;

    /** @var Profile $application */
    protected $profile;

    /** @var Thread $thread */
    protected $thread;

    /** @var Account $account */
    private $account;

    /**
     * ThreadStartedWithProfileNotification constructor.
     * @param Profile $profile
     * @param Thread $thread
     * @param Account $account
     */
    public function __construct(Profile $profile, Thread $thread, Account $account)
    {
        $this->profile = $profile;
        $this->thread = $thread;
        $this->account = $account;
    }

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

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject("{$this->account->name} Has Messaged You!")
            ->greeting("Hello")
            ->line("{$this->account->name} has messaged you on Digital Profile")
            ->action('View', Helper::url('/messaging'))
        ;
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            'uuid'  => $this->thread->uuid,

            'account' => [
                'uuid' => $this->account->uuid,
                'name' => $this->account->name,
                'type' => $this->account->type->key,
            ],
        ];
    }
}

标签: phplaravel

解决方案


在您的CreateController第 51 行中,您将一个关系对象 ( BelongsTo, Builder) 作为第一个参数发送给send非模型实例。

at UserNotification::send(object(BelongsTo), object(ThreadStartedWithProfileNotification))

UserNotification::send($profile->user(), new ThreadStartedWithProfileNotification(
    $profile,
    $thread,
    $accountSending
));

$profile->user()正在返回您定义为从对象的方法返回userProfile内容BelongsToreturn $this->belongsTo(User::class);。这不是模型,而是关系构建器实例。您可能想要模型,$profile->user或者$profile->user()->first(),而不是。

构建器具有对 PDO 实例的引用,因为他们使用 PDO 来构建查询。它们不是查询的结果。


推荐阅读