【问题标题】:Laravel Database Notifications Values not persisting in Model object when saving in database保存在数据库中时,Laravel 数据库通知值不会保留在模型对象中
【发布时间】:2017-09-21 11:12:04
【问题描述】:

所以我正在尝试将通知保存在数据库中。但是,一旦将具有值的模型对象传递给 Notification 类,数据就不会保留在其中,我会收到以下消息

照亮\数据库\查询异常(HY000) SQLSTATE[HY000]:一般错误:1364 字段'post_title'没有默认值(SQL:插入postsupdated_atcreated_at) 值 (2017-09-21 15:58:01, 2017-09-21 15:58:01))

现在我有了 Post_title 和 Post_description,但这里没有显示。

以下是我的通知类,奇怪的是,如果我转储 Post Object

,我会在构造函数中获取所有与帖子相关的信息
<?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,
        ];
    }
}

如果需要更多信息,请告诉我。 编辑:发布模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Feed\FeedItem;

/**
 * @property array|string post_title
 * @property array|string post_description
 * @property array|string is_featured
 * @property array|string is_rejected
 * @property mixed id
 */
class Post extends Model implements FeedItem
{
    use SoftDeletes;

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['deleted_at','created_at','updated_at','starting_time','ending_time'];
    protected $fillable = [
        'post_title', 'post_description', 'ebook_title', 'ebook_link', 'country_id', 'state_id', 'diary_id'
    ];

    public function user()
    {
        return $this->belongsTo('App\Models\User');
    }

    public function hashTags()
    {
        return $this->belongsToMany('App\Models\HashTag', 'hash_tag_post', 'post_id', 'hash_tag_id')->withTimestamps();
    }

    public function getRelatedHashTagsAttributes()
    {
        return $this->tags->pluck('id');
    }

    public function categories()
    {
        return $this->belongsToMany('App\Models\Category', 'category_post', 'post_id', 'category_id')->withTimestamps();
    }

    public function state()
    {
        return $this->belongsTo('App\Models\Category', 'state_id', 'id');
    }

    public function country()
    {
        return $this->belongsTo('App\Models\Category', 'country_id', 'id');
    }

    public function sliders()
    {
        return $this->belongsToMany('App\Models\Slider', 'slider_post', 'post_id', 'slider_id')->withTimestamps();
    }

    public function comments()
    {
        return $this->hasMany('App\Models\Comment');
    }

    public function postUploadedDatas()
    {
        return $this->hasMany('App\Models\PostUploadedData');
    }

    public function likes()
    {
        return $this->hasMany('App\Models\Like');
    }

    public function hasAction($user)
    {
        if ($this->likes()->where('user_id', $user)->first())
        {
            return true;
        }
        return false;
    }

    public function diaries()
    {
        return $this->belongsToMany('App\Models\Post', 'diary_post', 'post_id', 'diary_id');
    }

    public function getFeedItemId()
    {
        return $this->id;
    }

    public function getFeedItemTitle()
    {
        return $this->post_title;
    }

    public function getFeedItemSummary()
    {
        return $this->post_description;
    }

    public function getFeedItemUpdated()
    {
        return $this->updated_at;
    }
    public function getFeedItemAuthor() : string
    {
        return "";
    }

    public function getFeedItemLink()
    {
        return action('TravellersInn\PostController@getFeedItems', [$this->url]);
    }

    public function getTipsFeed()
    {
        return Post::where('contant_id','LIKE','%'.'3'.'%')->get();
    }
    public function getImagesFeed()
    {
        return Post::where('contant_id','LIKE','%'.'2'.'%')->get();
    }
    public function getVideosFeed()
    {
        return Post::where('contant_id','LIKE','%'.'4'.'%')->get();
    }
        public function getEbooksFeed()
    {
        return Post::where('contant_id','LIKE','%'.'6'.'%')->get();
    }



}

【问题讨论】:

  • 抱歉,您是说您正在传递post_titlepost_description 的值吗?这些总是需要设置吗?
  • 你能粘贴Post类吗?
  • 您是否将字段添加到模型fillable 属性中?

标签: php laravel-5.3 laravel-notification


【解决方案1】:

根据您的需要,有几个解决方案。

如果您总是想要 post_titlepost_description 的值,那么您需要添加一些验证并检查这些值是否正在传递给您的控制器以在数据库中设置,并且您的模型将填充这些值.

https://laravel.com/docs/5.5/validation

但是,如果标题和描述不总是设置,那么这可能是您的数据库,而不是您的代码。如果这些字段有时可能未被使用,那么您希望将每个字段的默认值设置为 '' 或 NULL。

类似的东西

ALTER TABLE &lt;table&gt; ALTER j SET DEFAULT '';

终于

Tbh 我认为你不需要那个构造函数,但我可能是错的。

【讨论】:

  • 您没有明白这一点,关键是我正在获取 description 和 title 的值,并且它们已经在 fillable 中。问题是我已经获得了帖子的价值。
【解决方案2】:

post_title 的值设置为 空白或更新架构以将默认值设置为 空白-&gt;default("");

如果您未设置为默认值,则始终需要列 post_title 任何值。

还要确保您在模型中添加了post_title

protected $fillable = ['post_title',....];// all columns 

【讨论】:

  • 您没有明白这一点,关键是我正在获取 description 和 title 的值,并且它们已经在 fillable 中。问题是我已经获得了帖子的价值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-04
  • 2018-04-26
  • 2019-04-26
  • 2018-05-30
  • 2011-09-17
  • 2021-09-27
相关资源
最近更新 更多