这是post.php模型
public function comments()
{
return $this->hasMany(Comment::class); //aquivalente a App\Comment
}
public function user()
{
return $this->belongsTo(User::class);
}
Public function addComment($body) //nombre de la variable post Task $task el segundo es la variable o los datos a que se va a pasasr
{
$user_id = \Auth::user()->id;
$this->comments()->create(compact(['body', 'user_id']));
}
Public function addreply($body)
{
dd($body);
$this->comments()->create(compact(['body', 'parent_id']));
}
这是commentController
class CommentsController extends Controller
{
public function store(Post $post)
{
$this->validate(request(), [
'body'=> 'required|min:5'
]);
$post->addComment(request('body'));
return back();
}
} 这是用于迁移表
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_id')->unsigned();
$table->integer('parent_id')->default(0);
$table->integer('user_id')->unsigned();
$table->string('body');
$table->timestamps();
});
Schema::table('comments', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropForeign('post_id');
Schema::dropForeign('user_id');
Schema::dropIfExists('comments');
}
所以当我发送评论或添加评论时,问题出在这里,显示user_id不是默认值。首先我添加了默认值,但它有问题
有人可以帮忙吗? 我想发送消息,其他用户可以回复消息 我想我必须添加一些代码但我呢?