2017-09-21 6 views
1

したがって、データベースに通知を保存しようとしています。しかし、値を持つモデルのオブジェクトが通知クラスに渡されると、データがそれに永続化されていないと私はメッセージLaravelデータベース通知データベースに保存するときにModelオブジェクトに値が保持されない

Illuminate \ Database \ QueryException (HY000) SQLSTATE[HY000]: General error: 1364 Field 'post_title' doesn't have a default value (SQL: insert into posts (updated_at , created_at) values (2017-09-21 15:58:01, 2017-09-21 15:58:01))

を以下の取得今私はPOST_TITLEとPost_descriptionを持っていますが、それらはここでは示されていません。続き

は、より多くの情報が必要な場合、私に教えてください、私はポストオブジェクト

<?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, 
     ]; 
    } 
} 

のダンプを取る場合は不思議な私は、コンストラクタ内のすべてのポストの関連情報を取得しています、私の通知クラスです。 EDIT:Postモデルデフォルトとして設定されていない場合post_titleは常に任意の値を必要とされている空白->default("");

列としてデフォルト値を設定するpost_titleとして、空白または更新スキーマの

<?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\[email protected]', [$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(); 
    } 



} 
+0

申し訳ありませんが、あなたは 'post_title'と' post_description'の値を渡していますか?これらは常に設定する必要がありますか? – dops

+0

'Post'クラスをペーストできますか? –

+0

フィールドを 'fillable'モデルのモデルに追加しましたか? – Nerea

答えて

0

のどちらかに置く値値。

はまた、あなたが望むものに応じて、このため解きのカップルがありますあなたのモデルでpost_title

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

あなたはポイントを取得していない、ポイントは私が説明とタイトルの値を取得しており、彼らはすでにfillableになっています。事は私がすでに投稿の価値を得ていることです。 –

1

を追加したことを確認してください。

post_titlepost_descriptionの値を常に使用する場合は、検証を追加し、値がコントローラに渡されてdbに設定され、モデルがこれらの値を満たすことを確認する必要があります。

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

を参照してくださいタイトルや説明が常にセットされていない場合は、これは潜在的にあなたのデータベースではなく、あなたのコードです。これらのフィールドが未使用になる可能性が高い場合は、各フィールドのデフォルトを "またはNULLに設定します。

ALTER TABLE <table> ALTER j SET DEFAULT '';

最後に

のようないくつかの事TBH、私はあなたがそのコンストラクタを必要とは思わないが、私は間違っている可能性があります。

+0

あなたはポイントを取得していない、ポイントは私が説明とタイトルの値を取得しており、彼らはすでにfillableになっています。事は私がすでに投稿の価値を得ていることです。 –

関連する問題