首页 > 解决方案 > 使用 saveMany Contents 存储通知数据

问题描述

所以我想做的是使用 Notifiable 将通知存储到数据库中,但我有一个 saveMany 内容,我不知道如何在通知中声明数据。从我声明的内容中得到一个空值,如下图所示

在此处输入图像描述

这是我的 saveMany 商店

$post = new Post;

        $post->user_id = Auth::guard('webcontrol')->user()->id;
        $post->lob = $request->lob;
        $post->type = 'post';
        $post->category_id = $request->category_id;
        $post->is_published = $request->is_published;
        $post->published_at = ($request->published_at) ? $request->published_at : Carbon::now();
        $post->metas = '{}';

        $post->save();

        $post->contents()->saveMany([
            new PostContent([
                'lang' => 'id',
                'slug' => str_slug($request->contents['id']['title'], '-'),
                'title' => $request->contents['id']['title'],
                'body' => $request->contents['id']['body'],
                'image' => $request->contents['id']['image'],
                'tag' => $request->contents['id']['tag'],
                'metas' => serialize($request->contents['id']['metas']),
            ]),

            new PostContent([
                'lang' => 'en',
                'slug' => str_slug($request->contents['en']['title'], '-'),
                'title' => $request->contents['en']['title'],
                'body' => $request->contents['en']['body'],
                'image' => $request->contents['en']['image'],
                'tag' => $request->contents['en']['tag'],
                'metas' => serialize($request->contents['en']['metas']),
            ])
        ]);

        Notification::send($user, new OneSignalNotification($post));
        dd('done');

这是我的通知

class OneSignalNotification extends Notification
{
    use Queueable;
    public $post;

    public function __construct($post)
    {
        $this->post = $post;
    }

    public function via($notifiable)
    {
        return ['database'];
    }
    
    public function toDatabase($notifiable)
    {
        return [
            'body' => $this->post['body'], //this is where i confused how to declare the saveMany contents so its not returning null
        ];
    }
}

标签: laravelnotifications

解决方案


所以我不确定你想要实现什么,但问题在于你想从帖子中检索正文,但在我看来, $post 没有 body 属性。PostContent 似乎有 body 属性,但问题是您将许多 PostContent 保存到同一个 $post 中,因此有多个 body 属性,每个 PostContent 一个。所以这就是为什么我建议使用 first() 方法从 $post 获取第一个 PostContent 然后返回它。如果要返回所有 body 属性,则必须获取所有这些属性并将它们添加到数组或字符串中,或​​者您想要的任何内容中。我希望这是有道理的。

你应该尝试使用这个:

$post = new Post;

$post->user_id = Auth::guard('webcontrol')->user()->id;
$post->lob = $request->lob;
$post->type = 'post';
$post->category_id = $request->category_id;
$post->is_published = $request->is_published;
$post->published_at = ($request->published_at) ? $request->published_at : Carbon::now();
$post->metas = '{}';

$post->save();

$post->contents()->saveMany([
    new PostContent([
        'lang' => 'id',
        'slug' => str_slug($request->contents['id']['title'], '-'),
        'title' => $request->contents['id']['title'],
        'body' => $request->contents['id']['body'],
        'image' => $request->contents['id']['image'],
        'tag' => $request->contents['id']['tag'],
        'metas' => serialize($request->contents['id']['metas']),
    ]),

    new PostContent([
        'lang' => 'en',
        'slug' => str_slug($request->contents['en']['title'], '-'),
        'title' => $request->contents['en']['title'],
        'body' => $request->contents['en']['body'],
        'image' => $request->contents['en']['image'],
        'tag' => $request->contents['en']['tag'],
        'metas' => serialize($request->contents['en']['metas']),
    ])
]);

Notification::send($user, new OneSignalNotification($post->fresh()));
dd('done');

然后在 Notification 内部,就像我说的,我们从 $post 中获取第一个 PostContent 并获取它的 body 属性:

class OneSignalNotification extends Notification
{
    use Queueable;
    public $post;

    public function __construct($post)
    {
        $this->post = $post;
    }

    public function via($notifiable)
    {
        return ['database'];
    }
    
    public function toDatabase($notifiable)
    {
        return [
            'body' => $this->contents()->first()->body,
        ];
    }
}

推荐阅读