laravel - 使用 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
];
}
}
解决方案
所以我不确定你想要实现什么,但问题在于你想从帖子中检索正文,但在我看来, $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,
];
}
}
推荐阅读
- python - 仅当条件满足使用模型中的数据时才显示 url 链接 - Django
- admin-on-rest - react-admin 中列出的项目数量是否有限?
- git - 如何减少git文件夹中的包文件?
- angular-material - multiTemplateDataRows,错误只有一个默认行没有when谓词函数
- javascript - 如何使用 Javascript eval 修改 Java 程序的变量?
- java - 随机图像生成器应用程序不想在 android studio 3.1.4 上工作
- java - Java - 带有空正文的 HttpURLConnection PUT 请求
- querydsl - QueryDSL 加入多个字段
- php - 如何在 PHP 中比较两个 JSON 文件
- node.js - Node Backend stay running in GPC Compute Engine