Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.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数据库通知值未在模型对象中持久化_Php_Laravel 5.3_Laravel Notification - Fatal编程技术网

Php 在数据库中保存时,Laravel数据库通知值未在模型对象中持久化

Php 在数据库中保存时,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

因此,我试图将通知保存在数据库中。但是,一旦将带有值的模型对象传递给通知类,数据就不会持久化,我将得到以下消息

照亮\数据库\查询异常(HY000) SQLSTATE[HY000]:一般错误:1364字段“post\u title”没有默认值(SQL:insert-into
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