Php 在数据库中保存时,Laravel数据库通知值未在模型对象中持久化
因此,我试图将通知保存在数据库中。但是,一旦将带有值的模型对象传递给通知类,数据就不会持久化,我将得到以下消息 照亮\数据库\查询异常(HY000) SQLSTATE[HY000]:一般错误:1364字段“post\u title”没有默认值(SQL:insert-intoPhp 在数据库中保存时,Laravel数据库通知值未在模型对象中持久化,php,laravel-5.3,laravel-notification,Php,Laravel 5.3,Laravel Notification,因此,我试图将通知保存在数据库中。但是,一旦将带有值的模型对象传递给通知类,数据就不会持久化,我将得到以下消息 照亮\数据库\查询异常(HY000) SQLSTATE[HY000]:一般错误:1364字段“post\u title”没有默认值(SQL:insert-intoposts(updated\u at), 在值处创建(2017-09-21 15:58:012017-09-21 15:58:01) 现在我有了帖子标题和帖子描述,但这里没有显示 下面是我的通知类,奇怪的是,如果我转储post
posts
(updated\u at
),
在值处创建(2017-09-21 15:58:012017-09-21 15:58:01)
现在我有了帖子标题和帖子描述,但这里没有显示
下面是我的通知类,奇怪的是,如果我转储post对象,我会在构造函数中获得所有与post相关的信息
<?php
namespace App\Notifications;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class PostCreated extends Notification
{
use Queueable,Notifiable;
protected $post;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($post)
{
$this->post = $post;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database'];
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'post_id' => $this->post->id,
'user_id' => $this->post->user_id,
];
}
}
将post\u title
的值设置为
空白,或更新架构以将默认值设置为
空白->默认(“”)代码>
列post\u title
始终需要任何值(如果未设置为默认值)
还要确保您已在模型中添加了post\u title
protected $fillable = ['post_title',....];// all columns
根据您的需要,有几个解决方案
如果您总是需要post_title
和post_description
的at值,则需要添加一些验证,并检查这些值是否已传递给要在db中设置的控制器,以及您的模型是否将填充这些值
看
但是,如果标题和描述不是始终设置的,那么这可能是您的数据库,而不是您的代码。如果这些字段有时可能未使用,则希望将每个字段的默认值设置为“”或NULL
比如
ALTER TABLE ALTER j SET DEFAULT'代码>
最后
Tbh我认为您不需要该构造函数,但我可能错了。对不起,您是说您正在为post\u title
和post\u description
传递值吗?这些是否总是需要设置?是否可以粘贴Post
类?是否将字段添加到模型filleble
属性中?您没有得到重点,重点是我正在获取描述和标题的值,它们已经在filleble中。问题是我已经得到了post的值。你没有得到重点,重点是我得到了description和title的值,它们已经可以填充了。问题是我已经得到了post的价值。
protected $fillable = ['post_title',....];// all columns