1

So i am trying to save Notifications in the database. But once an object of the model with values is passed to the Notification class, the data is not persisted in it and i get following message

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))

Now i have Post_title and Post_description but they are not shown here.

Following is my Notification Class, strangely i am getting all the post related info in the constructor if i take dump of 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,
        ];
    }
}

Let me know if more info is required. EDIT: Post model

<?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();
    }



}
noobie-php
  • 6,817
  • 15
  • 54
  • 101

2 Answers2

1

There are a couple of solves for this depending on what you want.

If you are always wanting at value for post_title and post_description then you need to add some validation and check that the values are being passed to your controller to be set in the db and that your model will fill these values.

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

However, if the title and description aren't always set then this is potentially your database, not your code. If these fields are likely to be unused at times then you want to set the default for each field to '' or NULL.

Some thing like

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

Finally

Tbh I don't think you need that constructor, but I could be wrong.

dops
  • 800
  • 9
  • 17
  • you did not get the point, the point is i am getting values for description and title and they are already in fillable . the thing is i am already getting values for post. – noobie-php Sep 21 '17 at 11:42
0

Either put value of post_title as blank or update schema to set default value as blank ->default("");

Column post_title is always needed any value if you not set as default value.

Also make sure you have added post_title in your model

protected $fillable = ['post_title',....];// all columns 
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
  • you did not get the point, the point is i am getting values for description and title and they are already in fillable . the thing is i am already getting values for post. – noobie-php Sep 21 '17 at 11:42