I'm building my own forum as practice, I set up Topic
and Comment
models with polymorphic relationship, a comment
can belong to a topic
or another comment
, this is because I want to have nested comments. Another issue is when I reply to the topic I get the following error:
SQLSTATE[HY000]: General error: 1364 Field 'parent_id' doesn't have a default value (SQL: insert into
comments
(user_id
,body
,commentable_id
,commentable_type
,updated_at
,created_at
) values (1 <\p>ljlnlnlnlnlnllnlnlnlnlnln</p>, 1, App\Models\Topic, 2019-11-08 20:41:43, 2019-11-08 20:41:43))
My models and Migrations.
Topic model:
class Topic extends Model
{
protected $table = 'topics';
public function author()
{
return $this->belongsTo('App\Models\User', 'user_id');
}
public function comments()
{
return $this->morphMany('App\Models\Comment', 'commentable')->whereNull('parent_id');
}
}
public function up()
{
Schema::create('topics', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id')->index();
$table->foreign('user_id')->references('id')->on('users');
$table->string('title');
$table->text('body');
$table->string('url')->unique();
$table->string('slug')->unique();
$table->boolean('isVisible')->default(false);
$table->timestamps();
});
}
Comment model:
class Comment extends Model
{
protected $table = 'comments';
public function author()
{
return $this->belongsTo('App\Models\User', 'user_id');
}
public function replies()
{
return $this->hasMany('App\Models\Comment', 'parent_id');
}
}
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id')->unsigned();
$table->integer('parent_id')->unsigned();
$table->integer('commentable_id')->unsigned();
$table->string('commentable_type');
$table->text('body');
$table->timestamps();
});
}
The error is pretty obvious; the column parent_id
doesn't have a value (you're not setting one, and there is no default). Either set the column as nullable
, or pass a parent_id
In migration
:
$table->integer('parent_id')->unsigned()->nullable();
Since a comment on a topic has no parent comment (not a reply) then the parent_id
would be null, however your database schema doesn't allow it, make the column nullable
$table->integer('parent_id')->unsigned()->nullable();
or give it null
as the default value
$table->unsignedInteger('parent_id')->default(null);
Hope this helps
i had this problem and solved by adding my column into fillable property
Add parent_id to the fillable array in your model Comment , to allow saving through create and massive methods
protected $fillable = ['parent_id'];
i am trying to insert data into database using laravel which gives me this error Illuminate \ Database \ QueryException (HY000) SQLSTATE[HY000]: General error: 1364 Field 'body' doesn't have a default value (SQL: insert into categories
( id
, title
, updated_at
, created_at
) values (1, khan, 2019-11-09 13:14:21, 2019-11-09 13:14:21)). plz instruct me what to do
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.