Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel:发送通知时不能序列化或取消序列化PDO实例_Php_Laravel - Fatal编程技术网

Php Laravel:发送通知时不能序列化或取消序列化PDO实例

Php Laravel:发送通知时不能序列化或取消序列化PDO实例,php,laravel,Php,Laravel,向通知中添加queueable时使用Laravel发送通知时出现问题。环顾了供应商类,但看不出为什么它会这样做,我甚至看不到任何地方对PDO的引用?如果有人能帮我们一把 堆栈跟踪: (1/1) PDOException You cannot serialize or unserialize PDO instances in Queue.php line 139 at PDO->__sleep() at serialize(object(SendQueuedNotifications))

向通知中添加queueable时使用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,
            ],
        ];
    }
}

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

at UserNotification::send(对象(BelongsTo)、对象(ThreadStartedWithProfileNotification))

$profile->user()
正在返回您定义从
profile
上的方法
user
返回的内容,该方法是
BelongsTo
对象:
返回$this->BelongsTo(user::class)。这不是一个模型,而是一个关系生成器实例。您可能需要模型,
$profile->user
$profile->user()->first()

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

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