当我DD每个变量时,我可以看到包括user_id在内的值 它只是不会存储在db ...我不确定是什么导致这个... userd_id总是以0值存储
查看:
{!! Form::open(['method'=>'POST', 'action'=>'UserCommentController@store']) !!}
<div class="form-group">
{!! Form::hidden('post_id', $post->id, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::hidden('user_id', $value = Auth::id(), ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::textarea('body', null, ['class'=>'form-control', 'rows'=> 2]) !!}
</div>
<br>
<div class="form-group">
{!! Form::submit('Submit Comment', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
控制器:
public function store(Request $request)
{
$data = [
'post_id' => $request->post_id,
'user_id' => $request->user_id,
'body' => $request->body
];
Comment::create($data);
return redirect()->back();
}
DB:
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_id')->unsigned()->index();
$table->integer('user_id')->unsigned()->index();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->text('body');
$table->timestamps();
});
}
答案 0 :(得分:0)
1)Auth::id()
有效吗?我不确定,但我总是使用Auth::user()->id
;
2)在评论控制器中尝试添加以下行:protected $fillable = ['user_id']
。如果您已经设置了$ fillable,只需添加&#39; user_id&#39;到阵列。这是我有时会忘记的事情。