Laravel 没有将所有数据插入数据库
Laravel not inserting all data to DB
我正在编写评论部分;我从下面的表格中获取用户数据:
<div class="comment">
<h2>Leave a comment</h2>
<form method="post" action="/blog/{{$post->id}}/comments">
{{csrf_field()}}
<input type="text" name= "name" class="textbox" value="Name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Name';}">
<input type="text" name="email" class="textbox" value="Email" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Email';}">
<textarea value="Message:" name="body" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Message';}">Message</textarea>
<div class="smt1">
<input type="submit" value="add a comment">
</div>
</form>
</div>
我正在通过 CommentsController 存储方法获取路线上的这些数据,如下所示:
Route::post('/blog/{post}/comments','CommentsController@store');
然后通过控制器的方法将它们存储在数据库中:`
public function store(Post $post){
Comment::create([
'body' => request('body'),
'email' =>request('email'),
'name' =>request('name'),
'post_id' => $post->id
]);
return back();
}
问题是,当我访问数据库时,正文字段完全可以插入,但是 post_id、姓名、电子邮件不会插入到数据库中,它们是空的。
我检查了我是否从 die;
dump
dd();
name
、email
和 $post->id
我从表单中得到的数据完全没问题,但我无法将它们插入到数据库中?
评论模型中受保护的 $fillable 数组中是否列出了 post_id、姓名和电子邮件列?如果他们没有被列为可填写的,那么他们将不会被输入。
当您在模型上使用 create()
方法时,您是在批量分配字段。因此,您需要在模型中设置 $fillable
属性 以及可以分配的所有字段,或者在模型中设置 $guarded
属性 以防止分配字段.确保只设置其中一个属性,而不是两个都设置。
在您的情况下,您应该将可填充 属性 设置为此。
protected $fillable = [
'name', 'email', 'body', 'post_id'
];
同时,当您像这样创建一个新模型时,您无需担心批量分配问题。
$comment = new Comment;
$comment->body = request('body');
$comment->email = request('email');
$comment->name = request('name');
$comment->post_id = $post->id;
$comment->save();
在此处阅读更多相关信息 https://laravel.com/docs/5.4/eloquent
我正在编写评论部分;我从下面的表格中获取用户数据:
<div class="comment">
<h2>Leave a comment</h2>
<form method="post" action="/blog/{{$post->id}}/comments">
{{csrf_field()}}
<input type="text" name= "name" class="textbox" value="Name" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Name';}">
<input type="text" name="email" class="textbox" value="Email" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Email';}">
<textarea value="Message:" name="body" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Message';}">Message</textarea>
<div class="smt1">
<input type="submit" value="add a comment">
</div>
</form>
</div>
我正在通过 CommentsController 存储方法获取路线上的这些数据,如下所示:
Route::post('/blog/{post}/comments','CommentsController@store');
然后通过控制器的方法将它们存储在数据库中:`
public function store(Post $post){
Comment::create([
'body' => request('body'),
'email' =>request('email'),
'name' =>request('name'),
'post_id' => $post->id
]);
return back();
}
问题是,当我访问数据库时,正文字段完全可以插入,但是 post_id、姓名、电子邮件不会插入到数据库中,它们是空的。
我检查了我是否从 die;
dump
dd();
name
、email
和 $post->id
我从表单中得到的数据完全没问题,但我无法将它们插入到数据库中?
评论模型中受保护的 $fillable 数组中是否列出了 post_id、姓名和电子邮件列?如果他们没有被列为可填写的,那么他们将不会被输入。
当您在模型上使用 create()
方法时,您是在批量分配字段。因此,您需要在模型中设置 $fillable
属性 以及可以分配的所有字段,或者在模型中设置 $guarded
属性 以防止分配字段.确保只设置其中一个属性,而不是两个都设置。
在您的情况下,您应该将可填充 属性 设置为此。
protected $fillable = [
'name', 'email', 'body', 'post_id'
];
同时,当您像这样创建一个新模型时,您无需担心批量分配问题。
$comment = new Comment;
$comment->body = request('body');
$comment->email = request('email');
$comment->name = request('name');
$comment->post_id = $post->id;
$comment->save();
在此处阅读更多相关信息 https://laravel.com/docs/5.4/eloquent